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

2015/4/22

guys how to call a method from another class i have a counter class and i want my game to change the speed after it dispay level2 so how do i call the score level to my apple class?

vela vela

2015/4/22

#
apple class
apple class:
 public void levelSpeed()
    {
        
        if(Score.Score==50)
        {
             setLocation(getX(),getY()+7);
        }
    }
counter class:
public class Counter extends Actor
{
    int score = 0;
    /**
     * 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, Color.GREEN, Color.BLACK));
        level();
    }
    
    public void addScore()
    {
        score++;
    }
    
    public void level()
    {
        if(score==50)
        {
            setImage(new GreenfootImage("LEVEL 2", 60, Color.WHITE, Color.BLACK));
        }
    }
     
danpost danpost

2015/4/22

#
If 'Score' in your apple class is a Counter object, then try changing line 5 to:
if (Score.score == 50)
Case (uppercase/lowercase) is important. The 'score' field in the Counter class begins with a lowercase, but you are trying to refer to it with uppercase, 'Score.Score'.
You need to login to post a reply.