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

2014/11/4

Shooting delay

Srax Srax

2014/11/4

#
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:
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);
        }
    }

}
Heres the code for the bullet (It can only move and disapear at edge of screen):
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);  
    }
    }
}
Srax Srax

2014/11/4

#
Help pls :)
danpost danpost

2014/11/4

#
You should try a search on 'shot timer' or 'shot delay timer'.
Srax Srax

2014/11/5

#
Okay, thanks.
Srax Srax

2014/11/5

#
danpost wrote...
You should try a search on 'shot timer' or 'shot delay timer'.
I can't really find any solutions, which works, can you please try to help me? :)
danpost danpost

2014/11/5

#
danpost wrote...
You should try a search on 'shot timer' or shot delay timer.
Using 'shot timer' as search string, an entry labeled 'Repeating Missile Shots, Delayed by danpost' came up under the 'Discussions' section which looked promising.
Srax Srax

2014/11/5

#
Okay, i'll try again, Thanks.
You need to login to post a reply.