开发者

Draw sprites between layers

Is it possible to draw a sprite, on a movieclip, on a spe开发者_C百科cific layer?

I have a MC I created in the Flash designer, and it has 3 layers on it, background, some layout stuff, and then some text. In actionscript i've created a Sprite object in code, drawn a rectangle on it, and added it to the stage with addChild (called from within the MC)

The problem is, the sprite is always being drawn above all the layout stuff I added in the designer.

Is there any way to add the sprite to the MC at a specific layer? In this case the background layer.

I tried changing the z on the sprite and no effect.


Changing the "z" was a nice try, but it will do nothing.

Actually, all the movie clips are stacked in a specific order. When there are drawn, it is done in that order, like a painter covering a graphic with a new one. Changing the "z" value will change the perceived depth of that movie clip, but it will not change the drawing order of the movie clips. As your sprite is on top of all the movie clip, it will always be the last one to be drawn, even if you change "z".

"Layers" do not actually exist when working with ActionScript. It is a concept only available through Flash Professional. In the end, Flash Professional converts its layers in addChild() requests.

So you can place your new movie clip with the addChildAt() method. With an index value of 0, the new movie clip will be drawn below all your other movie clips instead of the top.

You have to find the index of a movie clip of reference (e.g. by giving it an instance name in Flash Pro, "reference") so you can retrieve its index with "getChildIndex(reference)". Then, you can set your new movie clip on top or below this index with addChildAt(). If you set the same index as the movie clip of reference, it will be below it.

Another technique could be to create a kind of dummy movie clip ("dummy") that you set in a layer in Flash Professional, and then you add your new movie clip inside this dummy movie clip with "dummy.addChild()".

If you want to regroup several graphics through ActionScript in one "layer" to easily move the group up or down in the display list with addChildAt(), you can add them in a Sprite object, and then move the Sprite.


Covert the required objects on stage to movieclips. These movieclips could be on different layers. Then use the functions setChildIndex(), swapChildrenAt() and getChildIndex() to achieve the required effect. I can comment more if you show me the code. More details regarding the functions can be found at

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html

Also take a look at http://www.flashandmath.com/intermediate/depths/index.html


use addChildAt (sprite, index); the object that is at that index will go up one level.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜