So I made my shooting code (Spawns bullet once space is pressed) However, the problem is that it will spawn the object over and over if space is down. I tried using getKey, and now it will only spawn the bullet over and over if I'm only moving in one direction. Let's say it fixed it 50%, as it no longer does it when I move in multiple directions.
Moving code:
Shooting code:
public void playerMove()
{
int dx=0, dy=0;
if(Greenfoot.isKeyDown("up")) dy--;
if(Greenfoot.isKeyDown("down")) dy++;
if(Greenfoot.isKeyDown("right")) dx++;
if(Greenfoot.isKeyDown("left")) dx--;
if (dx == 0 && dy == 0) return;
switch(dx)
{
case -1:
setRotation(180-45*dy);
break;
case 0:
setRotation(180-90*dy);
break;
case 1:
setRotation(45*dy);
break;
}
move(speed);
} public void shoot()
{
if ("space".equals(Greenfoot.getKey()))
{
PlayerBullet playerbullet = new PlayerBullet();
getWorld().addObject(playerbullet, getX(), getY());
playerbullet.setRotation(getRotation());
}
}
