This site requires JavaScript, please enable it in your browser!
Greenfoot back
rachpaguia@gmail.com
rachpaguia@gmail.com wrote ...

2017/3/16

ADDING ACTORS

hello! how do you add an actor in the world every 10 seconds? thank you!
danpost danpost

2017/3/16

#
rachpaguia@gmail.com wrote...
hello! how do you add an actor in the world every 10 seconds? thank you!
Add a timer to track the time between each adding of the actor type:
1
2
3
4
5
6
7
8
9
10
private int spawnTimer;
 
private void checkForSpawning() // call from act method
{
    spawnTimer = (spawnTimer+1)%600; // repeat every 10 seconds (about)
    if (spawnTimer == 0) // at each timer reset
    {
        // code to add actor goes here
    }
}
This code will be best placed in your World subclass.
Thank you! How do you know what second corresponds to the percentage? I'm planning to add other actors too for every 3 and 5 seconds. I also want the added actors to be added at a certain place. Will this code work?
1
addObject(new A(), 150, Greenfoot.getRandomNumber(1));
All in all, my code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  private int spawnTimer;
     
  
private void checkForSpawning() // call from act method
 
 
 
{
    spawnTimer = (spawnTimer+1)%600; // repeat every 10 seconds (about)
    if (spawnTimer == 0) // at each timer reset
    {
         addObject(new A(), 150, Greenfoot.getRandomNumber(1));
    }
}
}
danpost danpost

2017/3/16

#
danpost wrote...
private void checkForSpawning() // call from act method
You need this:
1
2
3
4
public void act()
{
    checkForSpawning();
}
danpost danpost

2017/3/16

#
rachpaguia@gmail.com wrote...
How do you know what second corresponds to the percentage? I'm planning to add other actors too for every 3 and 5 seconds.
That is not a percentage -- that is a modulus operation which returns the remainder after dividing by the following value. So, the value of 'spawnTimer' will increment from 0 to 599 in 599 act cycles; then, on the 600th cycle, its value will return to zero to start over again.
I also want the added actors to be added at a certain place. Will this code work?
1
addObject(new A(), 150, Greenfoot.getRandomNumber(1));
Well, 'Greenfoot.getRandomNumber(1)' is always zero, '0'. You could simply replace the one with the other.
OK, so will my actor fall automatically, and add an object every 10 seconds with this code in my actor class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        public void act()
{
    checkForSpawning();
 
    {
        setLocation (getX(), getY()+7);
        Greenfoot.setSpeed(+30);// to move
        if (Greenfoot.isKeyDown("a"))
        {
           getWorld().removeObject(this); // removing A actor on 'a' press
           Greenfoot.playSound("poppy.wav");// playing 'pop' when A actor is removed
        }
  }
 
}
}
and this in my world class?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private int spawnTimer;
     
  
private void checkForSpawning() // call from act method
 
 
 
{
    spawnTimer = (spawnTimer+1)%600; // repeat every 10 seconds (about)
    if (spawnTimer == 0) // at each timer reset
    {
         addObject(new A(), 150, Greenfoot.getRandomNumber(1));
    }
}
}
danpost danpost

2017/3/16

#
rachpaguia@gmail.com wrote...
OK, so will my actor fall automatically, and add an object every 10 seconds with this code in my actor class
No. The 'act' method that calls 'checkForSpawning' should be in your World subclass along with the 'checkForSpawning' method. Remove lines 3 and 7 from the A class above.
Alright, here is my code now. Is it correct?
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
    /**
     * Constructor for objects of class bg.
     *
     */
    public bg()
    {    
        super(600, 400, 1);
        setBackground("Final ICT BG.png");
         
       
        GreenfootImage bg = new GreenfootImage("Final ICT BG.png");
        bg.scale(getWidth() +35, getHeight() +30);
        setBackground(bg);
    }
 
   private int spawnTimer;
     
     public void act()
{ checkForSpawning();
  
private void checkForSpawning()
 }// call from act method
 
 
 
{
    spawnTimer = (spawnTimer+1)%600; // repeat every 10 seconds (about)
    if (spawnTimer == 0) // at each timer reset
    {
         addObject(new A(), 150, Greenfoot.getRandomNumber(1));
    }
}
}
this is in my world code
danpost danpost

2017/3/16

#
rachpaguia@gmail.com wrote...
this is in my world code
That does not look like the act method I gave above. You are messing up your brackets again. Line 21 should be at line 25. Also, you can remove line 8. By convention, class names begin with uppercase letters -- so, 'bg' (for the class) should be 'Bg' (lines 5 and 0).
i suck at this i'm sorry, thank you so much. i'm sorry for giving you a headache
You need to login to post a reply.