views:

1077

answers:

1

Imagine I have a background and I want to show the background under the player object. This can be done with ease:

var player:Player = new Player();
addChild(player);

var background:Background = new Background();
addChildAt(background, 0);

However, imagine in this background I have transparent clouds which have to appear above the ship and non-transparent stars which need to appear under the ship. The above code would simply make all background objects go under the ship. Any tips?

+1  A: 

Make a foreground layer that is rendered after the Player object. That is the easiest way to accomplish this effect.

i.e.

var foreground:* = ...;
addChildAt(foreground, 2);

I'd imagine you're going to have multiple objects that you want to appear between the foreground and background layers, so I would actually also recommend creating an "active" layer, which is the actual parent of your "player" object.

So the object hierarchy looks akin to this:

Scene
    Background
        Rolling hills
    Active
        Player Sprite
        Enemies
        Obstacles
    Foreground
        Clouds
Walt W
Is this the only way? because this would force me to create several background engines for different objects as the background is generated dynamically (http://stackoverflow.com/questions/1342892/optimizing-my-dynamic-background-engine-for-a-2d-flash-game-in-actionscript-3).
Tom
.. which would hit performance by a lot.
Tom
Are you sure it would be a large performance hit? This seems like the sort of thing to try before optimizing prematurely. Since the objects in the background and foreground layers are not the same set of objects, there should be almost no difference between this and an approach that only had one background object.
Walt W