I am making a character walk. This code will make him wobble to the right and when thats done it will trigger him to wobble to the left and then call the function again to continue the loop.
I can get the loop to work fine by calling the function but how do I STOP the function? Also I want to call it later on. Is there a way to start and stop a function?
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();
OR IS THERE A BETTER WAY TO DO THIS? I am wanting to start the Tweens and stop them by calling a function. A TURN ON WALK AND TURN OFF. - THANKS SO MUCH!
Try out this code. Note how the Tween is reused, and the begin
position is reset to the Beau.rotation at the time that the Tween
motion restarts. Also, don't embed your functions within another function.
(I emphasized the motion and slowed it down for testing):
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();