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

2018/5/17

#
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

2018/5/22

#
Thanks!
Prox276 Prox276

2018/5/22

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

2018/5/22

#
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

2018/5/22

#
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

2018/5/22

#
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)
Prox276 Prox276

2018/5/29

#
Okay, Thanks, but I can't figure a thing out myself. I want to create another variable in a class called alien_1 which is exactly like the variable p in p1_1 but 1 number higher. So what do I enter in the alien_1 class?
Prox276 Prox276

2018/5/29

#
Or how can I ask, if "p" is 10 from the world class or the "alien_1" class?
danpost danpost

2018/5/29

#
Prox276 wrote...
Or how can I ask, if "p" is 10 from the world class or the "alien_1" class?
From world, similar to second code set above without the use of getWorld.
Prox276 Prox276

2018/5/29

#
Ok, thanks. But how do I do the first thing? I want the enemies to go faster everytime I kill one of them. I've already tried everything I could
danpost danpost

2018/5/29

#
Prox276 wrote...
how do I do the first thing? I want the enemies to go faster everytime I kill one of them.
Now that have provided a bit more information (the purpose of the field -- or how it is to be used), I can probably direct you little better. If you want all your enemies to use the same speed value, its field should be in the class of the enemy as a public static field. Your world constructor will need to zero the field. Let us say your alien_1 and alien_2 classes extend a class called Alien -- then, you would put the field (let us call it 'speed') in the Alien class and put the following line in your world constructor to initialize it:
Alien.speed = 1; // or whatever initial value it is to have
It must be initialized as indicated because otherwise its value from one game could be brought over to the next. Any class can access or change its value in the same way using class name-dot-field name notation.
Prox276 Prox276

2018/5/29

#
Ok, thanks a lot!
You need to login to post a reply.