我如何在舞台AS3上安排对象

时间:2016-03-09 11:24:17

标签: actionscript-3

我有一个拼图和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();
}

4 个答案:

答案 0 :(得分:2)

尝试以下操作。

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

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

答案 1 :(得分:1)

你可以做到

this.parent.addChild(this);

把这个'在所有父母的孩子的顶部

答案 2 :(得分:-1)

已经有一些非常好的答案,但为了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);
    }
    

答案 3 :(得分:-2)

我赞成@ SameerJain的回答,它会做你想要的。进一步使用 little ,您可以通过一个简单的功能减少所有重复的代码,使您的拼图可以拖动:

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