【问题标题】:"Depth" with moving Flash sprites带有移动 Flash 精灵的“深度”
【发布时间】:2015-01-11 06:07:39
【问题描述】:

我以前见过这种做法,但我不知道正确的术语...如何根据 Flash 精灵的位置、是否(或看起来像)来更改 Flash 精灵所在的“层”它位于精灵的前面或后面。

换句话说,如何使用 Flash sprites 实现“深度”?

看看我目前的问题:

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

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

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

【问题讨论】:

    标签: 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});
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多