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

2012/5/4

Delay Problems

Denzien Denzien

2012/5/4

#
I am making a game where the character hits at some point, here is the code:
 
       if(Greenfoot.isKeyDown("E"))
        {
           if(hand != 0)
           {
               setImage("actor_char_hit.png");
               Greenfoot.delay(7); Greenfoot.playSound("punch.wav");
               setImage("actor_char.png");            
           }
         }
But Greenfoot.delay() delays the whole game, not just the class, which makes any other moving character like an enemy delay as well, so how do I fix this?
ttamasu ttamasu

2012/5/4

#
You have to probably introduce a counter int punchCounter = 0; boolean playSound = false; ...... if (Greenfoot.isKeyDown("E")) if (hand != 0) { setImage("actor_char_hit.png"); punchCounter = 0; playSound = true } } .... public void act() { ..... if(punchCounter > 0) punchCounter --; else if (playSound){ playSound = false; Greenfoot.playSound("punch.wave"); setImage("actor_char.png"); } ... }
ttamasu ttamasu

2012/5/4

#
sorry having problems posting ... and a typo in the last post if (hand != 0) { setImage("actor_char_hit.png"); punchCounter = 7; // or number of cycles to delay playSound = true }
Denzien Denzien

2012/5/8

#
Thanks for this ^^
You need to login to post a reply.