이것은 형태이며, 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});
}
}
이 몇 가지 코드를 표시합니다. – Brian