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

2012/4/9

Bring Image To Front

RedPhoneBooth RedPhoneBooth

2012/4/9

#
Hey, I was wondering while making my adventure game, if it is possible to force an actor to go behind or in front of another. Because of the way I have coded my game, just changing the order in which the actors are added to the world would be problematic, so I was wondering if there was a way I could bring an actor to the front of the page, to allow another actor to go behind it (not talking about collision detection though). I could always just remove the actor that needs to go in front, and then re-adding it to the game, but that seems like an in-efficient solution. Thanks!
nccb nccb

2012/4/9

#
You can use setPaintOrder in the World class if you just want to affect appearance, and the two actors are of different classes. That's the only proper support Greenfoot has for deciding which actor appears in front of which.
bourne bourne

2012/4/9

#
If the actors are of the same class, you can remove them from the world and add them back in the order you wish.
RedPhoneBooth RedPhoneBooth

2012/4/10

#
Yea, but what about if I want the actor to appear in front of the trunks of trees, but behind the leaves (you can see what I mean if you play my simple adventure game), I could have two images, one of the trunks and one of the leaves, and do the paint order thing, but actors cant add objects to the world right? so how would I add leaves on top whenever the trunks are placed in the world. I could have a boolean that commands the world to place a new actor with the image set to the trees, but this too seems an inefficient solution! im sure theres a better way, or I could just make the tree sprite more like the pokemon tree, you know, with a wide base and kinda blocky, oh well
danpost danpost

2012/4/10

#
RedPhoneBooth wrote...
but actors cant add objects to the world right?
That is not quite true. As long as the actor is in the world, you can use the 'getWorld()' method to access the 'removeObject' and 'addObject' methods.
Leaves leaves = (Leaves) getOneIntersectingObject(Leaves.class);
if (leaves != null)
{
    int leavesX = leaves.getX();
    int leavesY = leaves.getY();
    getWorld().removeObject(leaves);
    getWorld().addObject(leaves, leavesX, leavesY);
}
You need to login to post a reply.