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

2022/11/22

New at programing in Greenfoot and need a help with a timer

Enigma321 Enigma321

2022/11/22

#
Firrst of all English is not my first language so if it s hard to express my self clearly. So i need to change images after 10 seconds on a actor. Itiried delay but it works partialy becoue it stops the entire game not just the process of changing images, i need a timer to work in background so the actors and the game dont stop moving.
  if (isTouching(Raketa.class))
    { 
    setImage("baby1.png");
    getWorld().removeObjects(getWorld().getObjects(Raketa.class));
    timer--;
 if(timer == 0) 
{
 setImage("baby2.png");
}
This is my solution to the problem but it doesnt work. if you can fix it or tell me how it would save my grade at school thanks.
danpost danpost

2022/11/23

#
Enigma321 wrote...
i need to change images after 10 seconds on a actor. Itiried delay but it works partialy becoue it stops the entire game not just the process of changing images, i need a timer to work in background so the actors and the game dont stop moving. << Code Omitted >> This is my solution to the problem but it doesnt work. if you can fix it or tell me how it would save my grade at school thanks.
You cannot run the timer in the block of code that initiates the timer. Maybe this will work:
if (isTouching(Raketa.class))
{
    setImage("baby1.png");
    getWorld().removeObjects(getWorld().getObjects(Raketa.class));
    timer = 600;
}
if (timer > 0 && --timer == 0)
{
    setImage("baby2.png");
}
Declare the timer (initialize it) with a zero value.
You need to login to post a reply.