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

2020/6/28

Continuous fire by holding down mouse

CodesForFun CodesForFun

2020/6/28

#
I'm trying to make the player fire by holding down the mouse button, but every code I find comes with its own problems, from only firing one bullet per click, to never stopping firing, to firing continuously only if the mouse is moving. I'm honestly a bit at a loss. Here's the code for the Player:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    private boolean shooting;
    private int cooldown;
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        movement();
        fireBullet();
    }    

    public void movement(){
        if(Greenfoot.isKeyDown("d")){
            setLocation(getX()+5, getY());
        }
        if(Greenfoot.isKeyDown("a")){
            setLocation(getX()-5, getY());
        }
        if(Greenfoot.isKeyDown("w")){
            setLocation(getX(), getY()-5);
        }
        if(Greenfoot.isKeyDown("s")){
            setLocation(getX(), getY()+5);
        }
        if(atWorldEdgeX()){
            if(getX()<10){
                setLocation(10, getY());
            } else {
                setLocation(getWorld().getWidth()-10, getY());
            }
        }
        if(atWorldEdgeY()){
            if(getY()<10){
                setLocation(getX(), 10);
            } else {
                setLocation(getX(), getWorld().getHeight()-10);
            }
        }
        MouseInfo mouse = Greenfoot.getMouseInfo(); 
        if (mouse != null) 
        {  
            turnTowards(mouse.getX(), mouse.getY());  
        }
    }

    public boolean atWorldEdgeX()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        else
            return false;
    }

    public boolean atWorldEdgeY()
    {
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }

    public void fireBullet()
    {
        MouseInfo mouseInfo = Greenfoot.getMouseInfo();
        if (cooldown == 0){
            if (mouseInfo == null) return; // no change
            if (shooting && (Greenfoot.mouseDragEnded(null) || Greenfoot.mouseClicked(null))) shooting = false;
            if (!shooting && Greenfoot.mousePressed(null)) shooting = true;
            if (shooting){
                getWorld().addObject(new Bullet(), getX(), getY()); // firing bullet
                Greenfoot.playSound("Laser_Shoot.wav");
                cooldown = 10;
            }
        } else {
            cooldown = cooldown -1;
        }
    }

}
danpost danpost

2020/6/28

#
Try this:
public void fireBullet()
{
    if (shooting && Greenfoot.mouseClicked(null)) shooting = false;
    else if (!shooting && Greenfoot.mousePressed(null)) shooting = true;
    if (cooldown > 0 && --cooldown > 0) return;
    if (shooting)
    {
        getWorld().addObject(new Bullet(), getX(), getY());
        Greenfoot.playSound("Laser_Shoot.wav");
        cooldown = 10;
    }
}
CodesForFun CodesForFun

2020/6/28

#
It worked, thanks!
You need to login to post a reply.