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

2013/5/26

Add multiple objects of an Action and move the at intervals dynamically

toscany toscany

2013/5/26

#
How can I add multiple objects of an Action class and move them at intervals dynamically
danpost danpost

2013/5/26

#
Do you mean place them in the world equidistance apart from each other?
toscany toscany

2013/5/26

#
I want to place them one after another at intervals and on the same starting point and they will move horizontally. @ danpost, Thanks. Hope the question is clear now
danpost danpost

2013/5/26

#
Ok, I will presume that you mean time intervals, since you specified the same starting point (meaning you did not mean distance intervals). You will need a timer to space out the spawnings. Since they will be moving once in the world, waiting a short time before spawning another will not place them on top of each other. In the world class, add an instance int 'spawnRate' field and create a method in the world class that its act method can call to run the timer and add the objects.
// instance field
private int spawnRate;
// add in 'act' method
checkSpawning();
// add this method
private void checkSpawning()
{
    spawnRate = (spawnRate+1)%100; // adjust last value to change the rate
    if (spawnRate == 0) addObject(new ActorName(), /*  xLocation, yLocation */ );
}
Put the movement code in the 'act' method of the 'ActorName' class (the class that is the blueprint for the object you want to spawn at intervals and move horizontally).
toscany toscany

2013/5/26

#
Thanks so much
You need to login to post a reply.