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

2015/2/18

How do I start movement after some seconds?

joandvgv joandvgv

2015/2/18

#
Hey! Well, I don't know how to do it. In my project, there are 5 routes that a truck must follow. If there are 5 trucks with 5 different routes, each one of them should start at the same point but in a different time. For example: truck 1 route 1, starts inmedeately. truck 2, route 2, though it is already created it should start 10 seconds after and truck 3 route 3 10 seconds then. I have no idea how to do so. What I've tried so far: Clock demo scenario. Using alerting methods for the world and actor. The world stops when I want but the actor never starts moving. You can find the scenario (very useful one) here. Double timer. I already had one timer for it pausing when reach some garbage in order to collect it and then starts again. Added another timer to inmediately pause and then start but I think I messed up with that and got some memory overflow error. In conclusion, what I ask for is: Any Idea on how to stop movement of just one actor right at the beginning of the simulation and then start it again? Thanks in advance
danpost danpost

2015/2/18

#
You do not need a Timer object for this, you just need a timer field (or fields): Option A: Place an int field in your world subclass for the timer; run the timer in the world subclass act method and inform the trucks when to roll out. The value of the timer can also be used to inform each truck which number it is. The class of the truck will need a boolean field to track the state of the truck 'rolling' or not. Option B: Inform each truck how long to wait before rolling out when created. This will involve passing a value to each truck in its constructor call. Plus, the class of the truck will then need the int timer field to run the waiting time. A boolean field for the state of the truck 'rolling' or not is not necessary in this case because the condition that the value of the timer is zero can be used in place of it. In either case, the movement code for the truck will be controlled by its 'rolling' state -- if rolling, then move.
fejfo fejfo

2015/2/18

#
I would use B like this
int startDelay

public Truck(int newStartDelay) {
    startDelay = newStartDelay
}

public void act() {
    if(startDelay > 0) startDelay--;
    else {
        your act code here
    }
}
joandvgv joandvgv

2015/2/18

#
Yes! Thank you. This did work. In my implementation I had to use a switch for 'switching' between different deleays depending on the routes. I'm creating other discussion because I need help now with other trouble and the getIntersectingObjetcs method.
You need to login to post a reply.