Hey, i have this code, which shoots out an actor by the name "Bullet".
The only thing the bullet do, is move forward.
When i hold down the shoot button (space), it rapidly shoots (Bullet). I want to add a timer, so it only shoots every second, if i hold down/keep pressing the shoot button (space).
Can anyone please help me? :)
Heres the code for the actor which shoots:
Heres the code for the bullet (It can only move and disapear at edge of screen):
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Spaceship extends Actor
{
/**
* Act - do whatever the Crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private float fireTimer;
public void act()
{
moveAndTurn();
eat();
if ("space".equals(Greenfoot.getKey()))
{
{
fire1();
fire2();
}
}
}
public void moveAndTurn()
{
move (0);
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+5);
}
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-5);
}
if (Greenfoot.isKeyDown("right"))
{
move(5);
}
if (Greenfoot.isKeyDown("left"))
{
move(-5);
}
}
public void eat()
{
}
private void fire1()
{
{
Bullet bullet = new Bullet();
getWorld().addObject(bullet, getX(), getY()+52);
}
}
private void fire2()
{
{
Bullet bullet = new Bullet();
getWorld().addObject(bullet, getX(), getY()-52);
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bullet extends Mover
{
/**
* Act - do whatever the Bullet wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(10.0);
if (this.atWorldEdge())
{
getWorld().removeObject(this);
}
}
}

