开发者

Actionscript 3: Place MovieClip on Specific Layer?

I am currently working on a platformer, and I have a bunch of different tiles I use to form my maps with. These tiles are very different in the looks, 开发者_StackOverflowand placed besides eachother, they give a very sharp change, which is not very visually appealing. I've been trying to fix this in the following way:

I have four layers, two for each block I'm fading. The first layer is where the actual tile, the graphic, is located. The second layer is the mask. In the mask layer, I have a gradient, which is the exact opposite of the other tiles mask layer (Mask layer 1 goes down, mask layer 2 goes up). However, I need to be able to alter those dynamically, place new gradients in the appropriate mask layer, and new tiles in the appropriate graphics layer.

I've been googling around for the last hour or so, attempting to find a way to define which layer a new MovieClip is placed on, but all I've found is setChildIndex, which, since one layer acts as a mask, I can't use. Is there any such function in Actionscript, or am I to make all combinations of tiles by hand?

Thank you for your help, -Birjolaxew


Try using something like this class:

package
{
    import flash.display.Sprite;

    public class LayeredContainer extends Sprite
    {
        // vars
        private var _layers:Array = [];

        /**
         * Constructor
         */
        public function LayeredContainer()
        {
            createLayer("_fallback_");
        }

        /**
         * Defines the layers to use
         * @param ...layers A list of layers to add
         */
        public function createLayers(...layers):void
        {
            var i:String;
            for each(i in layers)
                createLayer(i);
        }

        /**
         * Creates a single layer, adding it to the top of the display list
         * @param id The layer id
         */
        public function createLayer(id:String):Sprite
        {
            var l:Sprite = new Sprite();
            _layers[id] = l;

            addChild(l);

            return l;
        }

        /**
         * Returns a layer based on id
         * @param id The layer to return
         */
        public function layer(id:String):Sprite
        {
            return _layers[id] || layer("_fallback_");
        }
    }
}

Then you can create your layers and add things like this:

var lc:LayeredContainer = new LayeredContainer();
lc.createLayers("tiles", "whatever", "mask");

addChild(lc);

lc.layer("tiles").addChild(_some_tile_you_made_);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜