So, everytime all enemies are killed, 3 enemies spawn at a certain place. After that the score (variable "p") should change to 1 and it does, but if I press spacebar after iv'e killed all enemies and gotten one point, my points reset to zero. How can I fix this?
This is my code for the bullet :
And this is the code for the player :
Can somebody please help me? I was trying to fix this for the past hour.
import greenfoot.*;
public class projectile extends Actor
{
private int p;
public projectile()
{
p = 0; // This is what causes it, but I don't know how to fix it.
}
public void act()
{
getWorld().showText("Points " + p,300,200);
move(20);
if (isTouching(alien_1.class))
{
Greenfoot.playSound("hit.wav");
removeTouching(alien_1.class);
}
if (isTouching(alien_2.class))
{
Greenfoot.playSound("hit.wav");
removeTouching(alien_2.class);
}
if (getWorld().getObjects(alien_1.class).isEmpty())
{
p = p + 1;
Greenfoot.playSound("win.mp3");
getWorld().addObject(new alien_1(), 250, 200);
getWorld().addObject(new alien_1(), 450, 200);
getWorld().addObject(new alien_1(), 650, 200);
}
}
}
import greenfoot.*;
public class p1_1 extends Actor
{
private int rotation;
private int shotDelay;
public p1_1()
{
rotation = 4;
}
public void act()
{
if(Greenfoot.isKeyDown("space") == false)
{
setImage("p1sprite_1.png");
}
if (shotDelay > 0) shotDelay--;
if (Greenfoot.isKeyDown("A"))
{
setLocation(getX()-3, getY());
}
if (Greenfoot.isKeyDown("D"))
{
setLocation(getX()+3, getY());
}
if (Greenfoot.isKeyDown("W"))
{
setLocation(getX(), getY()-3);
}
if (Greenfoot.isKeyDown("S"))
{
setLocation(getX(), getY()+3);
}
if (isTouching(floor_01.class))
{
setLocation(getX()-3, getY());
}
if (isTouching(floor_03.class))
{
setLocation(getX()+3, getY());
}
if (isTouching(floor_00.class))
{
setLocation(getX(), getY()-3);
}
if (isTouching(floor_02.class))
{
setLocation(getX(), getY()+3);
}
if (Greenfoot.isKeyDown("right"))
{
setRotation(90);
rotation = 1;
}
if (Greenfoot.isKeyDown("down"))
{
setRotation(180);
rotation = 2;
}
if (Greenfoot.isKeyDown("left"))
{
setRotation(270);
rotation = 3;
}
if (Greenfoot.isKeyDown("up"))
{
setRotation(0);
rotation = 4;
}
if (shotDelay == 0 && Greenfoot.isKeyDown("space"))
{
if (Greenfoot.isKeyDown("space"))
{
Greenfoot.playSound("laser.wav");
shotDelay = 30;
fire();
setImage("p1sprite_2.png");
}
}
}
public void fire()
{
projectile bullet = new projectile();
getWorld().addObject(bullet, getX(), getY());
if (rotation == 3)
{
bullet.setRotation(180);
}
if (rotation == 2)
{
bullet.setRotation(90);
}
if (rotation == 4)
{
bullet.setRotation(270);
}
}
}
