"深度"移动Flash精灵

时间:2014-11-13 22:55:01

标签: actionscript-3 flash actionscript depth

我以前见过这个,但我不知道正确的术语......你怎么改变""" Flash精灵取决于它的位置,无论它是(或看起来)它是在精灵的前面还是后面。

换句话说,一个人怎样才能完成"深度"用Flash精灵?

看看我目前的问题:

enter image description here

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

我发现了这个:http://www.kirupa.com/forum/showthread.php?351346-Swap-depth-of-Movieclip-based-on-Y-location,但代码对我毫无帮助(我尝试使用它,但没有任何改变)。

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

enter image description here

1 个答案:

答案 0 :(得分:1)

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

我在wonderfl上为你做了一个演示:http://wonderfl.net/c/f54p

以下是源代码,其中包含一些注释:

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});
   }

}