2012-01-07 71 views
0

我正在进行角色散步。这段代码会使他向右摆动,当它完成时,它会触发他向左摆动,然后再次调用该函数以继续循环。在该功能中调用该功能并稍后停止循环 - 启动和追踪功能

我可以通过调用该函数来使循环正常工作,但是如何停止该函数? 另外我想稍后再调用它。有没有办法开始和停止一个功能?

function wobble() 
{ 
    var ws = .1; 
    var dis = 1; 

    var WobbleRight:Tween = new Tween(Beau, "rotation", Strong.easeIn, Beau.rotation, dis, ws, true); 
    WobbleRight.addEventListener(TweenEvent.MOTION_FINISH, WobbleL); 
    function WobbleL(e:TweenEvent):void 
     { 
     var WobbleLeft:Tween = new Tween(Beau, "rotation", Strong.easeIn,Beau.rotation, -dis, ws, true); 
     WobbleLeft.addEventListener(TweenEvent.MOTION_FINISH, WobbleR); 
      function WobbleR(e:TweenEvent):void 
      { 
      wobble(); 
      } 

     } 
} 
wobble(); 

,或是否有更好的方式来做到这一点?我想要启动补间并通过调用函数来阻止它们。开启WALK并关闭。 - 非常感谢!

回答

1

试用此代码。请注意Tween如何重用,并且在Tween运动重新启动时,begin的位置重置为Beau.rotation。另外,不要将你的函数嵌入到另一个函数中。

(我强调了运动和慢了用于测试):

import fl.transitions.TweenEvent; 
import fl.transitions.Tween; 
import fl.transitions.easing.Strong; 

var ws = 1; 
var dis = 10; 

var WobbleRight:Tween; 
var WobbleLeft:Tween; 

this.addEventListener(MouseEvent.CLICK, toggleWobble); 

function startWobble():void 
{ 
    WobbleR(null); 
} 

function WobbleL(e:TweenEvent):void 
{ 
    if (!WobbleLeft) 
    { 
     WobbleLeft = new Tween(Beau, "rotation", Strong.easeIn, Beau.rotation, -dis, ws, true); 
     WobbleLeft.addEventListener(TweenEvent.MOTION_FINISH, WobbleR); 
    } else { 
     WobbleRight.begin = Beau.rotation; 
     WobbleLeft.start(); 
    } 
} 


function WobbleR(e:TweenEvent):void 
{ 
    if (!WobbleRight) 
    { 
     WobbleRight = new Tween(Beau, "rotation", Strong.easeIn, Beau.rotation, dis, ws, true); 
     WobbleRight.addEventListener(TweenEvent.MOTION_FINISH, WobbleL); 
    } else { 
     WobbleRight.begin = Beau.rotation; 
     WobbleRight.start(); 
    } 
} 

function toggleWobble(e:MouseEvent):void 
{ 
    if(WobbleRight && WobbleRight.isPlaying) { 
     WobbleRight.stop(); 
    } else if (WobbleLeft && WobbleLeft.isPlaying) { 
     WobbleLeft.stop(); 
    } else { 
     startWobble(); 
    } 
} 

startWobble(); 
+0

惊人!非常感谢! – 2012-01-10 08:50:49

0

以什么方式你想停止摇摆?多次摇摆停止,或者用户应该点击一个按钮?

如果您想多次停止摆动,那么您应该使用计时器。在定时器事件中调用wobble()。如果用户停止摆动,则不仅仅是使用触发器isStopped。如果isStopped设置为true,则从摆动()返回。