2014-02-06 55 views
0

我不知道我想要的是最好用這個問題的標題來描述,但它是解釋我想要完成的最簡單的方法。如何在事件結束時觸發事件

因此,我試圖找出如何使一個_circle從一個角落移動到一個對象(它是在)的下一個角落,但該運動(的_circle)只有當_box對象(圓圈所在的那個)已停止移動,即它位於舞臺的中央。 而且在同一秒鐘_box正在上傳,_circle停止移動並保持它的位置,直到我再次點擊_button。

我只有移動_box元素的邏輯。 (甚至是邏輯太醜陋編碼/不是優雅)

private var _myBox:Sprite = new Sprite(); 
    private var _myCircle:Sprite = new Sprite(); 
    private var _myButton:Sprite = new Sprite(); 

    public function Test2() 
    { 
     addChild(_myBox); 
     _myBox.graphics.beginFill(0x0000FF); 
     _myBox.graphics.drawRect(0,0,300,100); 
     _myBox.graphics.endFill(); 
     _myBox.x = stage.stageWidth * 0.5 - _myBox.width * 0.5 ; 
     _myBox.y = 0 - _myBox.height; 

     _myBox.addChild(_myCircle); 
     _myCircle.graphics.beginFill(0x000000); 
     _myCircle.graphics.drawCircle(0,0,20); 
     _myCircle.graphics.endFill(); 
     _myCircle.x = 0 + _myCircle.width * 0.5; 
     _myCircle.y = _myCircle.height * 0.5 + _myCircle.height * 0.5; 

     addChild(_myButton); 
     _myButton.graphics.beginFill(0xAFAFAF); 
     _myButton.graphics.drawRect(0,0,100,100); 
     _myButton.graphics.endFill(); 
     _myButton.x = 400 
     _myButton.y = 300 
     _myButton.addEventListener(MouseEvent.CLICK, onClick); 

     var moveBoxDown:Timer = new Timer(1000, 1); 
     moveBoxDown.addEventListener(TimerEvent.TIMER, goingDown); 
     moveBoxDown.start(); 
    } 

    function goingDown(ev:TimerEvent):void 
    { 
     TweenLite.to(_myBox, 0.5 , {y: stage.stageHeight/2-_myBox.height/2 }); 
    } 

    private function onClick(ev:MouseEvent):void 
    { 
     var moveBoxUp:Timer = new Timer(1000, 1); 
     moveBoxUp.addEventListener(TimerEvent.TIMER, goingUp); 
     moveBoxUp.start(); 
    } 

    function goingUp(ev:TimerEvent):void 
    { 
     TweenLite.to(_myBox, 0.5 , {y: 0 - _myBox.height }); 
    } 

所以在我的問題的任何幫助嗎?或者使代碼更加令人滿意。應該是非常坦克的。

+0

我似乎記得TweenLite的擁有的onComplete處理程序。一旦廣場到達它的第一個位置,然後再次爲圓形,您可以使用它來啓動該圓圈。這樣你也可以擺脫定時器。 – InkeyString

+0

我剛看過。 delayedCall稍有不同。 onComplete是傳遞給補間的參數,與傳遞x和y參數的方式相同,即TweenLite.to(mc,1,{x:100,onComplete:myFunction});如果您需要傳遞函數參數,您可以:TweenLite.to(mc,1,{x:100,onComplete:myFunction,onCompleteParams:[「param1」,2]});這將執行myFunction一旦補間完成:) – InkeyString

+0

在特殊屬性下看看這裏http://www.greensock.com/as/docs/tween/_tweenmax.html – InkeyString

回答

1

試試這個

function goingDown(ev:TimerEvent):void 
{ 
    TweenLite.to(_myBox, 0.5 , {y: stage.stageHeight/2-_myBox.height/2, onComplete:onTweenComplete }); 
} 

function onTweenComplete(ev:Event = null) 
{ 
    //Do whatever after the tween has ended 
} 
+0

你的權利! – GregorII