Hello I'm trying to have my actor shoot when I click the mouse. If I hold down the left click it won't keep shooting unless my mouse is moving. If the mouse stays in one spot with the left click held down nothing happens. How can I fix this? The code is as follows.
I'll also put the bullet's code even though it doesn't matter since the bullet is being created in the Player's code.
public class Player extends Actor { int yVelocity; int xVelocity; MouseInfo mouse; public Player(){ getImage().scale(50, 50); } public void act() { mouse = Greenfoot.getMouseInfo(); turnTowardsMouse(); calcMotion(2, 1, 10); checkForFire(); } private void calcMotion(int accel, int decel, int speedCap){ boolean isYMotion = false; boolean isXMotion = false; if(Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")){ if(yVelocity > (speedCap * -1)){ yVelocity -= accel; } isYMotion = true; } if(Greenfoot.isKeyDown("s") || Greenfoot.isKeyDown("down")){ if(yVelocity < speedCap){ yVelocity += accel; } isYMotion = true; } if(Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")){ if(xVelocity > (speedCap * -1)){ xVelocity -= accel; } isXMotion = true; } if(Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")){ if(xVelocity < speedCap){ xVelocity += accel; } isXMotion = true; } if(yVelocity < 0 && !isYMotion){ yVelocity += decel; }else if(yVelocity > 0 && !isYMotion){ yVelocity -= decel; } if(xVelocity < 0 && !isXMotion){ xVelocity += decel; }else if(xVelocity > 0 && !isXMotion){ xVelocity -= decel; } setLocation(getX() + xVelocity, getY() + yVelocity); } private void turnTowardsMouse(){ if(mouse != null){ turnTowards(mouse.getX(), mouse.getY()); } } private void checkForFire(){ boolean isMouseDown; /* if(mouse != null){ isMouseDown = mouse.getButton() == 1; }else{ isMouseDown = false; } */ isMouseDown = Greenfoot.mouseDragged(null); if(Greenfoot.isKeyDown("space") || isMouseDown){ getWorld().addObject(new Bullet(getRotation(), 1, 5), getX(), getY()); } } }
public class Bullet extends Actor { int velocity = 1; int accel = 1; int maxSpeed = 10; public Bullet(int rotation, int x, int y){ setRotation(rotation); } public void act() { move(velocity); if(velocity < maxSpeed){ velocity += accel; } int x = getX(); int y = getY(); int width = getWorld().getWidth(); int height = getWorld().getHeight(); if(x == 0 || x == width - 1 || y == 0 || y == height - 1){ getWorld().removeObject(this); } } }