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

2014/5/20

Referencing a non-static variable in multiple world scenario

redandblue redandblue

2014/5/20

#
So I recently looked at tutorial 6 on how to call a method in one actor in a different actor's code. It was very helpful, but the directions say to make a reference to the world and then call up that reference in the actor and then the other actor's method. However, this method isn't working for me because I have 5 different worlds and they all must use this actor and recall this value. When I run the program, it won't work because the one actor cannot recall from only one world. Is there a way I can reference the variable in a different actor even when I have more than one world?
danpost danpost

2014/5/20

#
If you must, you can determine the current world using the 'instanceof' keyword -- 'if (getWorld() instanceof LevelOne) ((LevelOne)getWorld()).getWantedObject().runWantedMethod(); else if ... next world class check.
davmac davmac

2014/5/20

#
Is there a way I can reference the variable in a different actor even when I have more than one world?
You should perhaps explain a bit more what you really need, but one alternative is to maintain a reference to the other actor within the first actor itself (instead of in the world). Typically you might have the first actor create the second, and keep a reference to it in a variable.
redandblue redandblue

2014/5/20

#
I'm making a game where the main tile is a certain fruit and sometimes new fruits are released of the same type after an elapsed time. I have a fruit class and a random class. The fruits all have values based on their image file names. I reduced my number of levels to 4 for now and here is what I have for the new fruits to be released. Basically, the random tiles change into fruit tiles. Here is what I have,
1
2
3
4
5
6
7
8
9
10
public void newfruit()   
{   
    Random random = (Random)getOneObjectAtOffset(0, 0, Random.class); 
    if (getWorld() instanceof Level1) ((Level1)getWorld()).getFruit().getType();
    else if (getWorld() instanceof Level2) ((Level2)getWorld()).getFruit().getType();
    else if (getWorld() instanceof Level3) ((Level3)getWorld()).getFruit().getType();
    else if (getWorld() instanceof Level4) ((Level4)getWorld()).getFruit().getType();
    type = fruit.getType();
    setImage("random"+type+".png");   
}
I already have variables to return the values and this compiles fine but then it won't even run. Davmac is the reference maintained the same way? I can't use the addobject command in the actor.
davmac davmac

2014/5/20

#
In the code you post, where is 'fruit' declared and initialised? Why do you call 'getType()' and ignore what it returns? Perhaps you meant to declare a local variable called 'fruit' and assign to it depending on the world type, something like:
1
2
3
4
5
6
7
8
9
10
11
public void newfruit()     
{     
    Random random = (Random)getOneObjectAtOffset(0, 0, Random.class);   
    Fruit fruit = null;
    if (getWorld() instanceof Level1) fruit = ((Level1)getWorld()).getFruit();
    else if (getWorld() instanceof Level2) fruit = ((Level2)getWorld()).getFruit();
    else if (getWorld() instanceof Level3) fruit = ((Level3)getWorld()).getFruit();
    else if (getWorld() instanceof Level4) fruit = ((Level4)getWorld()).getFruit();
    type = fruit.getType(); 
    setImage("random"+type+".png");     
}     
I already have variables to return the values and this compiles fine but then it won't even run.
That's too vague.
Davmac is the reference maintained the same way? I can't use the addobject command in the actor.
It's a variable, same as it would be in the world, so all the same rules apply, yes. You can use 'getWorld().addObject(...)' from an actor.
You need to login to post a reply.