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

2014/1/5

Dynamically creating objects one at a time

xk1u xk1u

2014/1/5

#
I am trying to make a game with a boat with cargo. The idea is that boat arrives at the port with cargo so that the cargo can be transferred to land, and when that's done the boat can go on. For the beginning of the game the boat needs to "navigate" onto the screen to a certain location from the very left. So in a way that it seems the boat just arrives at the port. Now the boat is an certain actor and it has a move function which makes it navigating slowly untill it is at a certain location which is being checked with getX();. Now i want the boat to have 8 different cargo "containers" on it, which are being placed onto the boat the further it gets to the certain location. So everytime the boat travels 20 x-coordinates or so, there needs an new cargo object to be spawned. How can i do this?
You can make a "timer" that counts how many pixels the boat has moved since the last checkpoint. If it is a set point, you can do something like this:
/**Along with other Instance Variables*/
private int timer = 0;
private static int CHECKPOINT = 20;

/**Somewhere in you act menu*/
public void act()
{
...
if (timer != CHECKPOINT)
    timer++;
else
{
    timer = 0;
    methodThatSpawnsActor();
}
...
}
If you are making at different points (for example, one location is farther than the next), you can either make an array of all the checkpoints (by X location), and keep track of which location the boat is going to so you can access the right checkpoint, or if you're different locations are represented by an actor, you can see whenever the boat intersects one of these locations and add an actor when it first intersects for a period of time (that way you don't get a new object every pixel your boat moves over the location). Hope this helps!
You need to login to post a reply.