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

2013/9/25

Switching actor

Mishti Mishti

2013/9/25

#
I have a zebra and I need it to change into a tornado for like 10 seconds and then into a unicorn. But I can't get it to work. This is the code that I have for my Zebra; how can I change it make it work. public class Zebra extends Actor { public void act() { if(Greenfoot.isKeyDown("1")) { setImage("tornado.png"); Greenfoot.delay(10); setImage("unicorn.png"); } if(Greenfoot.isKeyDown("5") ) { setImage("explosion.png"); } } }
Gevater_Tod4711 Gevater_Tod4711

2013/9/25

#
You should try not to use Greenfoot.delay() but use a counter like this:
private int counter = -1;


public void act() {
    if (Greenfoot.isKeyDown(1)) {
        setImage("tornade.png");
        counter = 300;//this are acts not seconds. You have to try what are about 10 seconds.
    }
    if (counter > 0) {
        counter--;
        if (counter == 0) {
            setImage("unicorn.png");
        }
    }
}
Mishti Mishti

2013/9/25

#
it says that it can find the symbol counter :( what do I do?
danpost danpost

2013/9/25

#
I would have a Tornado class and a Unicorn class (as well as the Zebra class) and use the counter in the Tornado class which would add a new Unicorn object and remove itself after the timer has elapsed (just as the Zebra would add a new Tornado object and remove itself if the '1' key was pressed. In the above code, as long as the '1' key is being held, the counter will stay at 300. And, if the '1' key is pressed again before the counter reaches 0, it will reset back to 300. The explosion code may have to be moved (I do not know what state the Zebra object, that could be a tornado or unicorn with that code, should be in to allow the pressing of '5' to cause an explosion. Again, the explosion should be a different actor and when '5' is pressed, a new Explosion object should be added and the actor should remove itself.
You need to login to post a reply.