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

2021/3/3

how can I get the world from an empty class?

Who_TheGreen Who_TheGreen

2021/3/3

#
Hi, I wanted to ask if anybody knows a way to gain access of the world "MyWorld" from a custom class without using the help of an actor class. Then, I want to use the variables and the built-in methods from the world. Is the world able to pass itself to a public variable inside of the custom written class (that hasnĀ“t got built-in methods from Greenfoot)? Thank you very much in advance!
rocket770 rocket770

2021/3/5

#
I assume you're trying to create a reference to the world object without using getWorld()? In your world-class have this variable:
public static MyWorld world;
Then in the constructor of the world-class:
world = this;
So, in your custom class, you can have this as a variable:
MyWorld world;
And in the constructor of that custom class have this:
world = (MyWorld)MyWorld.world;
Replace all the "MyWorld" with the name of your world-class. In the custom class you should be able to call any world method such as world.getBackground().whatever() Hope this helps, let me know if I made a mistake anywhere or, if there's a simpler way to do it.
danpost danpost

2021/3/5

#
You can add a World type parameter to any method in your custom class that requires a reference to the world. That way, those methods are easily usable to multiple worlds.
Who_TheGreen Who_TheGreen

2021/3/14

#
Thanks a lot, rocket770! It works perfectly, helps me out a lot! Best regards, stay safe and stay healthy!, Who_TheGreen
You need to login to post a reply.