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

2013/1/7

worlds using methods from other worlds

bonana bonana

2013/1/7

#
Hi folks, I've got two worlds. One should be a introduction to explain the controls of my game and the second one is the actual game. Now I want my introduction to use a method in my world. I tried it with world1.method but it says non-static method cannot be referenced from a static context. Could somebody help me please? Also interesting to know: How can I tell my worlds which one has to be displayed first?
danpost danpost

2013/1/7

#
To have a world be the initially displayed world, manually instantiate an instance of that world (right click on the class icon of the world and select the 'new world1()' or whatever option from the list. The last world instantiated in this way will be the initial world for the scenario. It is possible to execute a method that is located in a world that is not currently running, however, it can only be run on a world of its type (in other words, if you had an 'updateBackground()' method in 'world1', you can run it on a world1 instance from world2, but you cannot run it on a world2 instance. You could however, make a method like:
public static void updateBackground(World world)
and place the code in world1 and call it from world2 with 'world1.updateBackground(this);' to update the background of world2 with a static method located in world1. Static methods are not executed on an object (whether it be a world or an actor) and are called class methods (as opposed to object instance methods; non-static methods). There is also a way to make a method available to both worlds without using static types, which involves creating a main world class that both the world1 and world2 are made sub-classes of. Just put the method in the super-class and both types of worlds can then use the method on instances of them.
bonana bonana

2013/1/8

#
In the introduction you have to choose a number of players (1-3) by pressing the button '1', '2' or '3'. These 3 keys let my introduction create a new world and activate a method in this world. In the class of this world are 3 methods one for 1 player, one for 2 and one for 3. Initiating the world is no problem, but I can't get the method activated. My code so far is:
if(Greenfoot.isKeyDown("1"))
        {
            Greenfoot.setWorld(new World1());
            World1.onePlayer();
        }
danpost danpost

2013/1/8

#
Instead of trying to call the method from outside the world, pass the number of players in a parameter to the new world and let it determine the method to run from there.
String key=Greenfoot.getKey();
if(key==null)return;
int index = "123".indexOf(key);
if(index !=-1)Greenfoot.setWorld(new World1(index+1));
Then in the World1 class, add a constructor with the parameter
bonana bonana

2013/1/8

#
Thanks a lot you just saved my life! :-) But at the same time you destroyed it... Now I have to rewrite all my classes to get everything running... Glad I have a backup! ^^ Is there no way to call the method like I did? :-(
danpost danpost

2013/1/8

#
You could try:
if(Greenfoot.isKeyDown("1"))
{
    World1 w1 = new World1();
    Greenfoot.setWorld(w1);
    w1.onePlayer();
}
bonana bonana

2013/1/8

#
Oh my god! It finally works! *__* Thank you danpost! <3 But... why does it work? :D It's very similar to my code...
danpost danpost

2013/1/8

#
The thing is, you cannot run the method on a class, it must be run on a world object. The program needs to know what world we are performing the method on (the class name is not enough as you can have several worlds of that type created -- though only one can be active at a time).
bonana bonana

2013/1/9

#
Ahh okay, understood. Thank you
You need to login to post a reply.