This site requires JavaScript, please enable it in your browser!
Greenfoot back
Verglas
Verglas wrote ...

2019/6/4

Spawn Monsters with Timer

Verglas Verglas

2019/6/4

#
Hi! I'm currently having trouble with accessing my Timer class in the world. I want to the world to access it so it will there will be increasing chances of spawning fastGhost and evilGhost as the Timer increases. Here is the code for my Timer class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Timer extends Actor
{
    /**
     * Act - do whatever the timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int minute;
    public int millisecond;
    public int second;
    private int counter = 1000;
    public void act()
    {
        time();
        setImage (new GreenfootImage(minute + ":" + second, 25,
        Color.RED, Color.BLACK));
    }  
     
     
    public void time()
    {
        while(counter > 0)
        counter -= 1;
        if(counter == 0)
        {
            millisecond++;
        }
        if(millisecond == 60)
        {
            millisecond = 0;
            second++;
        }
        if(second == 60)
        {
            second = 0;
            minute++;
        }
        counter = 60;
    }
     
}
Here is the spawnEnemy method in my world class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void spawnEnemy()
    {
        if (Greenfoot.getRandomNumber(120) < 1)
        {
            int spawnPlace = Greenfoot.getRandomNumber(8);
            int ghostType = Greenfoot.getRandomNumber(60);
            Actor ghost = null;
            if (ghostType < 45) ghost = new ghost();
            else if(ghostType < 55) ghost = new fastGhost();
            else ghost = new evilGhost();
            addObject(ghost, 50+100*spawnPlace, 40);
        }
         
    }
I want to make it so that when the minute becomes 2, it will increase the chances of spawning fastGhost and evilGhost, and further increasing the chances as the minute goes up. I'm having trouble with accessing the Timer in the world class.
Super_Hippo Super_Hippo

2019/6/4

#
You probably create and add the timer object to the world like this:
1
addObject(new Timer(), <x>, <y>);
Instead, you can save a reference to the object, so you can call methods on it whenever you want:
1
2
3
4
5
//outside methods
private Timer timer = new Timer();
 
//when adding it
addObject(timer, <x>, <y>);
Then, you can call methods on this object, for example: (you need to add that method in the Timer class then of course)
1
int minute = timer.getMinute();
Make sure to fix the "time" method though. It is not working as you want it.
Verglas Verglas

2019/6/4

#
Thanks! It worked after I add a getter in my timer class. For the time method, I am quite confused. The "act" method in a class is called 60x in a minute right? When I tried to increment to second every time the counter reached 0 from 60, turns out it's in millisecond. Which is why I did that.
Super_Hippo Super_Hippo

2019/6/4

#
Well, the problem is the "while". No matter what counter is at the beginning of the act method (1000 at the beginning or 60 every other time), it will be 0 after line 22. And yes, the act method is called around 50-60 times a second when the speed slider is at 50.
You need to login to post a reply.