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

2022/12/12

How to get Variables of other Actors?

LR6549 LR6549

2022/12/12

#
I want to make a score for the collected Items, but the following code for the score is not changing even though you´ve collected new items. (The Variable in the Player class is : int collects = 0; and it gets changed by 1 when collecting an Item) import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Score here. * * @author (your name) * @version (a version number or a date) */ public class Score extends Player { int score = 0; /** * Act - do whatever the Score wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { String text = "Score: " + score; GreenfootImage image = new GreenfootImage(text, 20, Color.WHITE, new Color(0,0,0,0)); this.setImage(image); int score = ((Player)getWorld().getObjects(Player.class).get(0)).collects; } }
LR6549 LR6549

2022/12/12

#
If found it out by testing, I had to remove the _int_ in int score = ((Player)getWorld().getObjects(Player.class).get(0)).collects; so it would not create this variable over and over again. (What I think)
Jestry Jestry

2022/12/12

#
Yes you are right. U simply created another Integer with the exact name like your attribute "score" of the class. Therefore u never change "your" score.
danpost danpost

2022/12/13

#
LR6549 wrote...
public class Score extends Player
The Score object is not a Player object. That is, by having Score extend Player, you are saying that any Score object will be a player. It will have all the attributes of a player and all the behavior of one before being appended/modified by the code in the Score class. Score should extend some class that is a more general object than a Score object. This could be something like GUI or Component or, simply, Actor -- which is most probably the best in your case. Also, you are creating an image every act step for the Score object. This is not necessary as the score changes at various instances of time, not all the time. Better might be the following:
import greenfoot.*;

public class Score extends Actor
{
    private int score;
    
    public Score()
    {
        updateDisplay();
    }
    
    public void adjustScore(int amt)
    {
        score += amt;
        updateDisplay();
    }
    
    public void updateDisplay()
    {
        String text = "Score: " + score;
        GreenfootImage image = new GreenfootImage(text, 20, Color.WHITE, new Color(0,0,0,0));
        setImage(image);
    }
}
Then, your Player class can use the follow at the time when the player does collect something:
((Score)getWorld().getObjects(Score.class)).adjustScore(1);
If it is necessary to retrieve the actual value of collected items, you might want to add the following to the Score class:
public int getScore()
{
    return score;
}
You need to login to post a reply.