通过单击更改舞台上对象的位置 - ActionScript 3

时间:2011-10-31 17:53:49

标签: actionscript-3 movieclip

我在舞台上有somme mc,我希望通过点击它们来改变mc与antoher。例如,如果我点击mc1而不是mc2,那么他们可以改变位置。 任何想法如何做到这一点? 谢谢你的时间

1 个答案:

答案 0 :(得分:0)

您需要为movieclip设置一个click事件,并将其记录在单击movieclip的变量上,然后在单击第二个时,您只需交换它们的位置。我会给你一段应该有用的代码,应该足以告诉你它是如何完成的。

import flash.events.MouseEvent;
// Variable that will be used to store the 1st clicked MC
var lastClickedSwapMC;

//First we define the function to be called
function clickEventSwapMcs(evt : MouseEvent) {
    // Verify if a mc wasn't previously clicked
    if(lastClickedSwapMC == null) {
        // If it wasn't, it's the 1st time, so store the MC that was clicked
        lastClickedSwapMC = evt.currentTarget;
    } else {
        // If it was, we just need to swap the positions of the stored one with the one just clicked
        var savedX : Number = evt.currentTarget.x;
        var savedY : Number = evt.currentTarget.y;
        evt.currentTarget.x = lastClickedSwapMC.x;
        evt.currentTarget.y = lastClickedSwapMC.y;
        lastClickedSwapMC.x = savedX;
        lastClickedSwapMC.y = savedY;
        //After swaping their position, we clear the last clicked MC
        lastClickedSwapMC = null;
    }
}

//Now we register the click event on them so it calls the function
mc1.addEventListener(MouseEvent.CLICK, clickEventSwapMcs);
mc2.addEventListener(MouseEvent.CLICK, clickEventSwapMcs);