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

2014/6/1

How can I make a timer that will after 5 seconds remove the first bullet

Nib Nib

2014/6/1

#
How can I make a timer that will after 5 seconds remove the first bullet World world = getWorld(); private void fire() { if(Greenfoot.isKeyDown("space")) { f++; if((f==1) && (k<7)) { Fire fire = new Fire(); k++; getWorld().addObject(fire, getX(), getY()); fire.setRotation(getRotation()); fire.move(0.0); Greenfoot.playSound ("Bang.wav"); timer--; } if(f==100) f=0; } else f=0; }
lordhershey lordhershey

2014/6/2

#
You can set a timer on the bullet itself to remove itself when the time expires like this:
public class thingie extends actor{

long startTime = 0;

public void act(){
if (startTime < 1)
{
   startTime = System.currentTimeMillis();
}
long endTime = System.currentTimeMillis();

if ((endTime - startTime) > 5000)
{
  getWorld().removeObject(this);
  return;
}

//rest of act method here
}
}
You need to login to post a reply.