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

2013/1/30

updating the score variable

sametguzelgun sametguzelgun

2013/1/30

#
Hey. I wrote the code seen below:
import greenfoot.*;
import java.awt.Color;
public class Score extends Actor
{
    public int score;
    public Score() 
    {
        setImage(new GreenfootImage("Score: "+score,18,Color.BLACK,Color.GREEN));
    }
    public void addScore(int pn)
    {
        pn = pn;
        score = score + pn;
    }
    public int getScore()
    {
        return score;
    }
    public void update()
    {
        setImage(new GreenfootImage("Score: "+score,18,Color.BLACK,Color.GREEN));
    }
}
And the methods I use to create a class that did not see the new value. Why is this?
Super_Hippo Super_Hippo

2013/1/30

#
Maybe you have to add the method update in the method addScore:
    public void addScore(int pn)
    {
        pn = pn;
        score = score + pn;
        update();
    }
sametguzelgun sametguzelgun

2013/1/30

#
Thank you for your attention. But I still have the same problem. During the study does not show the new value.
import greenfoot.*;
import java.awt.Color;
public class Add extends Actor
{
    public Add()
    {
        setImage(new GreenfootImage("Press Space",18,Color.BLACK,Color.GREEN));
    }
    public void act() 
    {
        Score s = new Score();
        if(Greenfoot.isKeyDown("space"))
        {
            s.addScore(1);
        }
    }    
}
This is the code I tried to do the update.
danpost danpost

2013/1/30

#
You do not need another class (the 'Add' class in this case) to update the 'Score' class. Remove the 'Add' class and change the 'Score' class to the following:
import greenfoot.*;
import java.awt.Color;

public class Score extends Actor
{
    public int score;

    public Score() 
    {
        update();
    }

    public void addScore(int pn)
    {
        score = score + pn;
        update();
    }

    public int getScore()
    {
        return score;
    }

    public void update()
    {
        setImage(new GreenfootImage("Score: "+score, 18, Color.BLACK, Color.GREEN));
    }
}
You need to login to post a reply.