【问题标题】:how I can arrange the object on the stage AS3如何在 AS3 舞台上布置对象
【发布时间】:2016-06-23 17:57:12
【问题描述】:

我有一个拼图和 9 张图片,所以当我开始拖动任何图片时,我希望它高于剩余的所有图片。

如何在 Action Script 3.0 上做到这一点? 例如:

import flash.events.Event;

Puz3_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz3);
function StartPuz3 (e:Event):void
{
    Puz3_Level7A.startDrag();
}
Puz2_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz2);
function StartPuz2 (e:Event):void
{
    Puz2_Level7A.startDrag();
}
Puz1_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz1);
function StartPuz1 (e:Event):void
{
    Puz1_Level7A.startDrag();
}

【问题讨论】:

  • 对不起,我是说 AS3 Action script 3.0
  • 对不起,这是我的第一个问题,所以我不知道所有的角色
  • 您应该阅读显示列表的工作原理,here for example 最简单的方法是将您想要的精灵放在显示列表的最后。例如使用swapchildren()
  • 好的,但是如何将它与 startDrag 和 StopDrag 一起使用?
  • 嗯,什么时候有意义?每次在 Sprite 上调用 startdrag 之前,请在显示列表中交换该 Sprite,以便将其放在顶部。

标签: actionscript-3


【解决方案1】:

尝试关注。

import flash.events.Event;

Puz3_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz3);
function StartPuz3 (e:Event):void
{
    this.setChildIndex(Puz3_Level7A, this.numChildren - 1);
    Puz3_Level7A.startDrag();
}
Puz2_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz2);
function StartPuz2 (e:Event):void
{
    this.setChildIndex(Puz2_Level7A, this.numChildren - 1);
    Puz2_Level7A.startDrag();
}
Puz1_Level7A.addEventListener (MouseEvent.MOUSE_DOWN, StartPuz1);
function StartPuz1 (e:Event):void
{
    this.setChildIndex(Puz1_Level7A, this.numChildren - 1);
    Puz1_Level7A.startDrag();
}

此代码假设所有上述精灵都是同一个精灵的孩子。

【讨论】:

  • 友情提示,您应该了解event.currentTarget 属性。这将使这段代码更加简洁。这样,您可以为所有对象只使用一个事件处理程序。
  • 可能有更好的方法,AS3 有很多方法 :) @aaron 已经提出了一种简洁的方法。我刚刚解释了 OP 在代码中存在的问题。
【解决方案2】:

你可以这样做

this.parent.addChild(this);

把'this'放在所有父母的孩子的顶部

【讨论】:

    【解决方案3】:

    已经有一些非常好的答案,但为了 DRY(不要重复自己)和完整性,这是我对此的看法。

    这样做的一种干净的方式,包括停止拖动。没有内联函数或不必要的重复代码。

    //add the mouse down listener to all objects you want to be able to drag.
    //if you have a lot, you could also do this in a loop to make it more concise.
    Puz3_Level7A.addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
    Puz2_Level7A.addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
    Puz1_Level7A.addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
    
    var currentPiece:Sprite; //a var to hold a reference to item last mouse downed (to use in the mouse up handler)
    
    //the mouse down handler to drag a piece
    function dragPuzzlePiece(e:Event):void
    {
        currentPiece = e.currentTarget as Sprite; //the event's current target is a reference to the item you added the listener to.  We are casting it as a Sprite so the compiler knows what kind of object it is.
    
        addChild(currentPiece); //this will bring it to the top. 
        //setChildIndex(currentPiece, numChildren-1); //this would do the same as above
        //swapChildren(currentPiece, getChildAt(numChildren-1); //this would do the same as above
        currentPiece.startDrag(); //start dragging the piece that was clicked
    
        //now listen for the mouse up event to stop dragging the piece
        //we listen on the stage, as sometimes dragging can lag when moving the mouse quickly and the mouse may not be over the item when you release the button
        stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    }
    
    function mouseUpHandler(e:Event):void {
        //remove the mouse up listener
        stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    
        //stop the drag:
        if(currentPiece) currentPiece.stopDrag();
    
        //do something with currentPiece now that the drag is done.
          //like make sure the piece is in a valid spot
          //check if the puzzle is complete now
    
        puzzleDone(); //let's say you call this function at some point
    }
    
    function puzzleDone():void {
        currentPiece = null; //clear this var so objects can be garbage collected
        //!important
        //when you use setChildIndex, or AddChild, or SwapChildren 
        //or anything that modifies the parentage of a object
        //that object will no longer be removed by timeline keyframes
        //it will stick around until it's parent is removed, or you explicitly remove it.
    
        //to that end, you'll want to manually remove your puzzle pieces when done
        removeChild(Puz3_Level7A);
        removeChild(Puz2_Level7A);
        removeChild(Puz1_Level7A);
        //you can use some of patterns below for this as well.
    
    }
    

    关于将鼠标按下监听器添加到所有拼图块中,如果您有很多拼图块,这里有一些其他选项可以更好地扩展:

    1. 如果这些片段都是 FlashPro 实例名称,您可以这样做:

      //loop three times
      for(var i:int=1;i<=3;i++){
         //get the child dynamically from the name (
         var piece:DisplayObject = getChildByName("Puz" + i + "_Level7A");
         if(piece){
             piece.addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true); 
         }
      }
      
    2. 如果您的拼图是共同父母的唯一孩子(假设此代码在该父母的时间线上),您可以这样做:

      //loop backwards through all children of this
      var i:int = this.numChildren;
      while(i--){
          this.getChildAt(i).addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
      }  
      
    3. 如果你有你的作品在一个数组中:

      var allPieces:Array = [Puz3_Level7A, Puz2_Level7A, Puz3_Level7A];
      
      for(var i:int=0;i<allPieces.length;i++){
          allPieces[i].addEventListener(MouseEvent.MOUSE_DOWN, dragPuzzlePiece, false, 0, true);
      }
      

    【讨论】:

    • 为什么不创建一个Puzzle 类而不是这个时间线代码?
    • @Aaron - 这很容易成为类代码。我非常不同意它“没用”。 ——
    【解决方案4】:

    我赞成@SameerJain 的回答,它会做你想做的事。进一步一点,您可以使用一个简单的函数来减少所有重复的代码,让您的拼图可以拖动:

    function makeDraggable(target:Sprite):void {
        target.addEventListener(MouseEvent.MOUSE_DOWN, dragStart);
    }
    
    function dragStart(e:MouseEvent):void {
        var target:Sprite = e.currentTarget as Sprite;
        target.startDrag();
        setChildIndex(target, numChildren - 1);
        stage.addEventListener(MouseEvent.MOUSE_UP, dragEnd);
    }
    
    function dragEnd(e:MouseEvent):void {
        stopDrag();
        stage.removeEventListener(MouseEvent.MOUSE_UP, dragEnd);
    }
    
    makeDraggable(Puz3_Level7A);
    makeDraggable(Puz2_Level7A);
    makeDraggable(Puz1_Level7A);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 2016-06-18
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多