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

2012/12/12

I have a problem with scoreboard

DaBist DaBist

2012/12/12

#
I have a class named Label:
public class Label extends vse
{  

    private String text;

    public void act() 
    {

        Integer i = new Integer(Points);
        text = "Tocke: " + i.toString();
        int stringLength = (text.length() + 2) * 10;

        setImage(new GreenfootImage(stringLength, 16));
        GreenfootImage image = getImage();

        updateImage();
    }

    public void PointsLabel(int Points)
    {
        Integer i = new Integer(Points);
        text = "Money: " + i.toString();
        int stringLength = (text.length() + 2) * 10;

        setImage(new GreenfootImage(stringLength, 16));
        updateImage();
    }

    public void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.setFont(new Font("Tahoma", Font.BOLD, 12));
        image.setColor(Color.WHITE);
        image.drawString(text, 1, 12);
    }

}
a class called vse:
public class vse extends Actor
{
    public int Points;

    public int pristej()
    {
        Points++;
        return Points;
    }
and a class called gumb1:
public class gumb1 extends vse
{

    /*public int Points; 
    
    public gumb1 ()
    {
        Points = 0;
    }*/
    
    public void act() 
    {
        onClick();
    }    
    public void onClick()
    {
        if(Greenfoot.isKeyDown("A"))
        {
          Actor gumbek = getOneObjectAtOffset (0, 0, pikca.class);
          if(gumbek != null)
          {
              World world;
              world = getWorld();
              world.removeObject(gumbek);
              Greenfoot.playSound("Đđđ.wav");
              pristej();
          }
        }
    }
    
   /* public int getPoints()
    {
        return Points;
    }*/

}
The problem is, that displayed points are always: Tocke: 0. In the object gumb1 points are increasing, but when i call them for display, not working. Any ideas?
vonmeth vonmeth

2012/12/12

#
You are never actually calling the Points from your gumb1 object. Label has its own local Points value which is never updated. You need to create a way for Label to 'get' your gumb1 object, and then retrieve gumb1 object's local Points value, and then set that retrieved value to Label's local Points value.
DaBist DaBist

2012/12/12

#
could you post how the code should look like? because i can't manage to make it work ...
danpost danpost

2012/12/12

#
If you want only one counter (Points) in the vsa class that all its subclasses share, you can use the 'static' keyword in the field declaration in the vsa class:
public static int Points;
My only concern is: why do you have a class and two subclasses for something that can be done in one single class; as only one has an image.
DaBist DaBist

2012/12/13

#
danpost wrote...
If you want only one counter (Points) in the vsa class that all its subclasses share, you can use the 'static' keyword in the field declaration in the vsa class:
public static int Points;
My only concern is: why do you have a class and two subclasses for something that can be done in one single class; as only one has an image.
it works now. Thanks!
You need to login to post a reply.