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

2017/1/24

Incompatible types

Solomon Solomon

2017/1/24

#
public class Counter extends Actor { int score = 10; /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setImage(new GreenfootImage("Score : " + score, 24 )); } } at score i get incompatible types: java.lang.string can not be converted to int
Super_Hippo Super_Hippo

2017/1/24

#
Try to use:
setImage(new GreenfootImage("Score : " + score,  24, null, null ));
Solomon Solomon

2017/1/25

#
ok it s working but when i place it it s just the default greenfoot img shouldnt it be score : 10?
Super_Hippo Super_Hippo

2017/1/25

#
No, when you place it, no image is created → the default green foot icon shows up. To avoid that, the class code should look like this:
private int score = 10;

public Counter()
{
    updateImage();
}

public void addScore(int amount)
{
    score += amount;
    updateImage();
}

private void updateImage()
{
    setImage(new GreenfootImage("Score : " + score, 24, null, null));
}
'public Counter' is the constructor which will be executed when the object is created (so it will show the image immediately and not only after pressing 'run'). It only has to update the image when the score changed ('addScore' method, maybe you want to name that differently) and not every act cycle. No act method is needed.
You need to login to post a reply.