2014-11-13 1 views
0

이전에이 방법을 사용 해봤지만 올바른 용어를 모르겠습니다 ... 플래시 스프라이트가있는 위치에 따라 "레이어"를 어떻게 변경합니까? 그것이 스프라이트의 앞이나 뒤쪽에있는 것처럼 보입니다.움직이는 플래시 스프라이트를 사용하는 "깊이"

즉, 플래시 스프라이트로 어떻게 "깊이"를 달성 할 수 있습니까?

내 현재의 문제에서 살펴 보자 : 위쪽으로 걸을 때 한 북극곰은 다른 모든 북극곰의 상단에 얼마나

enter image description here

참고.

나는 이걸 건너왔다 : http://www.kirupa.com/forum/showthread.php?351346-Swap-depth-of-Movieclip-based-on-Y-location,하지만 코드는 도움이 안된다. (나는 그것을 사용해 봤지만 아무것도 바뀌지 않았다.)

난 (그 위치에 따라 어떻게 달팽이 이동 뒤에 스프라이트 앞에 주) 수행 할 작업의 예이다 :

enter image description here

+0

이 몇 가지 코드를 표시합니다. – Brian

답변

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

} 
관련 문제