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

2017/3/16

Cannot find method

SeaShore SeaShore

2017/3/16

#
I'm trying to have an actor access a method written in the world class, but I'm receiving a cannot find symbol-message error. Any idea what the problem is ?
public class GameCounter extends Actor
{
   public int numberCorrect;
   public int getNumberCorrect()
    {
        return numberCorrect;
    }
    
    public void checkNumberCorrect()
    {
        if (getNumberCorrect()==1)
        {
            World world = getWorld();
            world.endGame();
        }
    }
}

public class MyWorld extends World
{
public void endGame()
    {
        showText("Score:" + score.getScore() + "/16", getWidth()/2, getHeight()/2);
        Greenfoot.stop();
    }
}
danpost danpost

2017/3/16

#
Line 14 will not find the 'endGame' method in the World class and will not look for it in your MyWorld class until you reference the world as a MyWorld type world. Change line 13 to:
MyWorld world = (MyWorld)getWorld();
You need to login to post a reply.