【问题标题】:"Depth" with moving Flash sprites带有移动 Flash 精灵的“深度”
【发布时间】:2015-01-11 06:07:39
【问题描述】:
【问题讨论】:
标签:
actionscript-3
flash
actionscript
depth
【解决方案1】:
这是 z 排序的一种形式。基本上,您在 AS3 中需要做的就是将您的 Sprites 存储在一个数组中,并根据每个精灵的 y 位置对其进行排序。然后循环遍历数组并将精灵连续添加到舞台或另一个DisplayObject。您可能会认为这样添加它们不好,但这是正常的做法。
我在 Wonderfl 上为你做了一个演示:http://wonderfl.net/c/f54p
这里是一些cmets的源代码:
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});
}
}