Hello, i got a problem. I want to remove an Object after 10 seconds but i dont want to use Greenfoot.delay(10) because it pauses the game.
My Code:
I already tried it like this but it doesnt worked for me :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import greenfoot.*; public class NPC1 extends NPC { public void act() { move(); if (Greenfoot.isKeyDown( "e" ))reden(); } public void move(){ if (random1()== 1 ){ setLocation(getX()- 2 , getY()); setRotation( 180 ); } if (random1()== 2 ){ setLocation(getX()+ 2 , getY()); setRotation( 0 ); } if (random1()== 3 ){ setLocation(getX(), getY()- 2 ); setRotation( 270 ); } if (random1()== 4 ){ setLocation(getX(), getY()+ 2 ); setRotation( 90 ); } } public void reden(){ Text errormsg= new Text(); getWorld().addObject(errormsg, 600 , 220 ); errormsg.setText( "You cannot take coke with a penny" ); //getWorld().removeObject(errormsg); } private int random1(){ int random1 = Greenfoot.getRandomNumber( 150 )+ 1 ; return random1; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import greenfoot.*; public class NPC1 extends NPC { int cooldown = 0 ; public void act() { move(); cooldown--; reden(); } public void move(){ if (random1()== 1 ){ setLocation(getX()- 2 , getY()); setRotation( 180 ); } if (random1()== 2 ){ setLocation(getX()+ 2 , getY()); setRotation( 0 ); } if (random1()== 3 ){ setLocation(getX(), getY()- 2 ); setRotation( 270 ); } if (random1()== 4 ){ setLocation(getX(), getY()+ 2 ); setRotation( 90 ); } } public void reden(){ if (Greenfoot.isKeyDown( "e" )){ Text errormsg= new Text(); getWorld().addObject(errormsg, 600 , 220 ); errormsg.setText( "You cannot take coke with a penny" ); cooldown = 70 ; if (cooldown< 1 )getWorld().removeObject(errormsg); } } private int random1(){ int random1 = Greenfoot.getRandomNumber( 150 )+ 1 ; return random1; } } |