I am struggling on finding a way to delay an attack in my game. I have a character that is moving, and I want it to stop moving for a few moments before it can move again. I tried googling solutions but i cant seem to find a way to do this.I think its because of the way I arranged the code, because im really new to Greenfoot. How can I add this delay?
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 49 50 51 52 53 54 55 56 57 58 59 | import lang.stride.*; import java.util.*; import greenfoot.*; /** * */ public class Person extends Actor { private boolean isAttacking = false ; /** * */ public Person() { GreenfootImage image = getImage(); image.scale( 500 , 500 ); setImage(image); } /** * Act - do whatever the Person wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ final public void act() { int health = 100 ; int damage = 5 ; int power = 0 ; long lastAdded = System.currentTimeMillis(); if (Greenfoot.isKeyDown( "right" ) && ! isAttacking) { setLocation(getX() + 5 , getY()); } if (Greenfoot.isKeyDown( "left" ) && ! isAttacking) { setLocation(getX() - 5 , getY()); } long curTime = System.currentTimeMillis(); if ( "x" .equals(Greenfoot.getKey())) { isAttacking = true ; atac(); Greenfoot.delay( 5 ); } } /** * */ public void atac() { Attack attack = new Attack(); getWorld().addObject(attack, getX() + 50 , getY()); } } |