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

2018/1/31

Variable and Points

Cett Cett

2018/1/31

#
Im having a really bad time trying to get a points system to stick. Chloe is my main character and everytime she touches a Mushroom I want her to earn a point, here's that bit of code:
1
2
3
4
5
6
7
8
9
10
11
public void checkCollision()
    {
        if (isTouching(Mushroom.class))
        {           
            removeTouching(Mushroom.class);
             
            Points p = new Points();
            p.addScore(1);
 
        }
}
That code links to this segment:
1
2
3
4
5
6
7
8
9
10
public void addScore(int point)
    {
        score = score + point; 
        System.out.println(score);
    }
     
    public int getScore()
    {
        return score;
    }
Then there's a bit later where the getScore is called in the world. However, when I actually go to interact with a Mushroom the score remains stuck at 1? There's no other code in the Chloe class that interacts with Points and Points only establishes the Private variable of score. Thanks.
valdes valdes

2018/1/31

#
The problem is line 7 of the first code. Every time yoy detect a collision, you create a new Points object (a new score), therefore it is always 1. Move that line below the declaration of the class. p must be a member variable
Cett Cett

2018/1/31

#
okay cool, that makes sense. do you know how I would fix that?
valdes valdes

2018/1/31

#
Move the line below the declaration of the class
1
2
3
4
5
6
7
public class Chloe extends Actor {
 
  private Points p = new Points();
 
  //methods and act
 
}
Cett Cett

2018/1/31

#
oh my god that's so simple. Thank you so much!
You need to login to post a reply.