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

2013/1/13

"Non-static method cannot be referenced from a static context" - help?

Athanasia Athanasia

2013/1/13

#
Hey there everyone! As we're supposed to do a project for informatics over the holidays and I'm really stuck with a problem, I hope for your help. For explaining: My game is based on a Spaceship being supposed to destroy some enemies. When all enemies are destroyed, I want the game to start kind of a "new level" with new enemies and stuff. Therefore, I wrote the following part in my Spaceship-act-method:
if(SpaceWorld.enemyCounter == 0)
        {
            SpaceWorld.reachedNewLevel();
            System.out.println("Next Level reached!");
        }
On this point I encountered the first problem as it said "Non-static method cannot be referenced from a static context". So I changed the "reachedNewLevel"-method in the world class to:
public static void reachedNewLevel()
{
 removeObjects(getObjects(null));
}
Well, now I get the same message, just for "getObjects", which obviously cannot be used in a static context. And... yeah. Well. What do I do now? Is my idea of letting the SpaceWorld delete all Objects and after that construct a new scenario even possible? If yes, how do I avoid this problem I have here? If no, is there another possible way? Please apologize if it's a dumb question, we did not do this for too long. And also, English is not my first language, so maybe I've made some little mistakes... Thank you! :)
Gevater_Tod4711 Gevater_Tod4711

2013/1/13

#
The problem is that the method removeObjects is a non-static one. You should try to use it as a non-static mehtod:
if(SpaceWorld.enemyCounter == 0) {  
    ((SpaceWorld) getWorld()).reachedNewLevel();  
    System.out.println("Next Level reached!");  
} 

//and in your world class:
public void reachedNewLevel() {
    removeObjects(getObjects(null));
} 
You need to login to post a reply.