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

2018/5/17

Score counter won't work

Prox276 Prox276

2018/5/17

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

And this is the code for the player :
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);
   }
   }
}


Can somebody please help me? I was trying to fix this for the past hour.
danpost danpost

4 days ago

#
There is no variable 'p' until you create projectile object -- and each projectile object will have a unique variable 'p' that will contain a distinct value pertinent only to that actor. I suggest the variable be moved elsewhere (like maybe to your World subclass).
Prox276 Prox276

10 hours ago

#
Thanks!
Prox276 Prox276

10 hours ago

#
But how can I make it, so that other actors can read/change the variable "p"?
danpost danpost

10 hours ago

#
Prox276 wrote...
But how can I make it, so that other actors can read/change the variable "p"?
Add public getter and setter methods in the class p is in:
public int getP()
{
    return p;
}

public void setP(int value)
{
    p = value;
}
All the actors need now is a reference to whatever object holds p.
Prox276 Prox276

9 hours ago

#
Ok, so now i've pasted the code in the beginning of the code of my actor "player1". So, what do I write in another actors code so that the actor changes p by (as example) 1?
danpost danpost

8 hours ago

#
Prox276 wrote...
Ok, so now i've pasted the code in the beginning of the code of my actor "player1". So, what do I write in another actors code so that the actor changes p by (as example) 1?
Depends. If during colliion detection:
Actor actor = getOneIntersectingObject(P1_1.class);
if (actor != null)
{
    P1_1 p1 = (P1_1) actor;
    int p = p1.getP();
    // interim code possibly to adjust p
    p1.setP(p);
}
Without collision detection, you will have to go through the world:
if (!getWorld().getObjects(P1_1.class).isEmpty())
{
    P1_1 p1 = (P1_1)getWorld().getObjects(P1_1.class).get(0);
    // etc (continuing at line 5 above)
You need to login to post a reply.