Actionscript 3 &引用;“深度”;用移动的闪光精灵

Actionscript 3 &引用;“深度”;用移动的闪光精灵,actionscript-3,flash,actionscript,depth,Actionscript 3,Flash,Actionscript,Depth,我以前见过这样做,但我不知道正确的术语…如何更改Flash精灵所在的“层”,取决于它在哪里,它是(或看起来像)在精灵的前面还是后面 换言之,如何用Flash精灵实现“深度” 看看我目前的问题: 注意,当向上行走时,一只北极熊是如何在其他北极熊之上的 我遇到过这样的情况:,但代码对我没有帮助(我尝试过使用它,但没有任何改变) 这是我想要完成的一个示例(请注意蜗牛如何根据其位置在精灵前后移动): 这是一种z排序形式。基本上,在AS3中需要做的就是将精灵存储在一个数组中,并根据每个精灵的y位置对其进

我以前见过这样做,但我不知道正确的术语…如何更改Flash精灵所在的“层”,取决于它在哪里,它是(或看起来像)在精灵的前面还是后面

换言之,如何用Flash精灵实现“深度”

看看我目前的问题:

注意,当向上行走时,一只北极熊是如何在其他北极熊之上的

我遇到过这样的情况:,但代码对我没有帮助(我尝试过使用它,但没有任何改变)

这是我想要完成的一个示例(请注意蜗牛如何根据其位置在精灵前后移动):


这是一种z排序形式。基本上,在AS3中需要做的就是将
精灵存储在一个数组中,并根据每个精灵的
y
位置对其进行排序。然后在阵列中循环并将精灵连续添加到舞台或另一个
DisplayObject
。您可能会认为这样添加它们很糟糕,但这是正常的方式

我在wonderfl上为您制作了一个演示:

下面是带有一些注释的源代码:

package {
import flash.display.Sprite;
import flash.events.*;

public class FlashTest extends Sprite {

    private var boxes:Array;
    private var BOX_NUM:int = 20;
    public function FlashTest() {
        var box:Box;

        boxes = [];

        // create a bunch of boxes and add them
        for(var i:int = 0; i < BOX_NUM; i++){
           box = new Box();
           boxes.push(box);
           addChild(box);   
        }
        addEventListener(Event.ENTER_FRAME, onLoop);
    }

    private function onLoop(e:Event):void{
        // sort boxes based on the y property of each sprite
        boxes.sortOn("y", Array.NUMERIC);    
        for(var i:int = 0; i < BOX_NUM; i++){
          // scale the boxes so the effect is easier to see
          boxes[i].scaleX = boxes[i].scaleY = boxes[i].y / stage.stageHeight + 0.5;
          addChild(boxes[i]);    
        }
    }
  }
}

import flash.display.Sprite;
import com.greensock.TweenLite;
import flash.events.*;

class Box extends Sprite {
   public function Box() {
       graphics.beginFill(0);
       graphics.lineStyle(3, 0xFF0000);
       graphics.drawRect(0,0,50,50);
       addEventListener(Event.ADDED_TO_STAGE, onAdded);

   }   

   private function onAdded(e:Event):void{
     // randomize the box position
     x = Math.random() * stage.stageWidth;
     y = Math.random() * stage.stageHeight;
     animate();
   }
   // animate to a random place on the screen over and over
   private function animate():void{
        var rx:Number = Math.random() * stage.stageWidth,
            ry:Number = Math.random() * stage.stageHeight;
       TweenLite.to(this, 3, {x : rx, y : ry, onComplete : animate});
   }

}
包{
导入flash.display.Sprite;
导入flash.events.*;
公共类FlashTest扩展了Sprite{
专用变量框:数组;
私有变量框的数量:int=20;
公共功能测试(){
var-box:box;
框=[];
//创建一组框并添加它们
对于(变量i:int=0;i