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

2014/2/5

Detect which world an actor is in?

Entity1037 Entity1037

2014/2/5

#
This seems like a simple one, but I can't seem to figure it out. It doesn't work when I do this:
1
2
3
if (getWorld() instanceof LevelEditor){
    //...
}
I cannot seem to find any resources that could help me Either. Does anyone know?
davmac davmac

2014/2/5

#
If you mean you want to check the class of the world the actor is in, the code you have written above is fine. If it doesn't seem to be working it's because something is wrong elsewhere in your code.
Entity1037 Entity1037

2014/2/6

#
I don't see anything wrong in my coding; the statement is at the start of the act() method. The code in the if statement just doesn't activate for some reason, and I just can't figure it out! (I forgot to publish the source code to my game, so I can't give you the entire code at this time).
danpost danpost

2014/2/6

#
What is the code inside the 'if' block supposed to do?
Entity1037 Entity1037

2014/2/6

#
Fill and fillRect a 25 by 25 image (which would be invisible otherwise).
danpost danpost

2014/2/6

#
Are you setting this image to an actor? is that actor in the world? (of course if you are setting the image of the actor that this class is executing the act method on, then yes; otherwise, maybe it is not the actor you see in the world whose image is changing)
davmac davmac

2014/2/6

#
I don't see anything wrong in my coding; the statement is at the start of the act() method. The code in the if statement just doesn't activate for some reason, and I just can't figure it out!
I guess nobody can figure it out unless you show them the code ;)
Entity1037 Entity1037

2014/2/6

#
The code is in the constructor. Hold on, I'll be able to publish the code in a bit...
danpost danpost

2014/2/7

#
The actor is not in a any world when the constructor is being executed -- the actor has to be created before it can be placed in a world. Override the 'addedToWorld(World)' method and move the code into it.
Entity1037 Entity1037

2014/2/7

#
Aw....... *facepalm* OK, thank you!
You need to login to post a reply.