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

2016/3/7

Start a void in the World from an actor

Zyon900 Zyon900

2016/3/7

#
Hello, my problem is, that I want to execute a void that is written in a world, from an actor in the world(for the reason, that I have many variables that are saved in worlds, and I need this to get them in the world, where the Actor is, to set a specific world) the code in the actor, that should execute the void in the World:
public void act() 
    {
        if(Greenfoot.mouseClicked(this)==true)
        {
            getWorld().door_left;
        }
    }
the "door_left" is the void it should execute. I hope you can help me. Thanks in advance.
danpost danpost

2016/3/7

#
First things first -- if 'door_left' in line 5 is a method call (or, calling a 'void', as you put it), then that name must be followed by a set of parenthesis ( 'door_left()' ). The 'getWorld' method of the Actor class returns an object of World type. In order for the 'door_left' method, which is in your subclass of World (not in the World class or the Object class), to be found, you must specify what type of World the world is. That is, if the method is in a class called MyWorld, then you must specify so:
World world;
world = getWorld();
MyWorld myWorld;
myWorld = (MyWorld)world;
// or, simply
MyWorld myWorld = (MyWorld)getWorld();
Your method call could simply be written as follows (if MyWorld was the name of the World subclass):
((MyWorld)getWorld()).door_left();
Zyon900 Zyon900

2016/3/8

#
My problem is the actor can be in different Worlds, that are called level_1.level_2,....,level_13 I have an array in another World where i have saved the worlds like level=new level_3(); or level=new level_10(); if that helps. Thanks in advance
danpost danpost

2016/3/8

#
Zyon900 wrote...
My problem is the actor can be in different Worlds, that are called level_1.level_2,....,level_13 I have an array in another World where i have saved the worlds like level=new level_3(); or level=new level_10(); if that helps.
Actually, the array will not help unless the actor knows about the world that array is in. My suggestion is to create a single superclass for all your level classes so that no matter which subclass the world is, you can cast it to the superclass to access your methods and fields. You can look at the structure of my Super Level Support Class scenario or even use the Level class within it as the superclass of all your levels and structure yours the same way.
Zyon900 Zyon900

2016/3/13

#
I have it , the worlds it will be in, check if a boolean is true, and the actor is setting the boolean true
danpost danpost

2016/3/13

#
Zyon900 wrote...
I have it , the worlds it will be in, check if a boolean is true, and the actor is setting the boolean true
Are you saying you fixed it with this? or are you continuing with your explanation of your issue and it is not yet fixed?
You need to login to post a reply.