在该函数内调用该函数,然后停止循环 - 启动和停止停止功能

发布于 2024-12-25 08:37:15 字数 800 浏览 2 评论 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();

或者有更好的方法吗?我想启动补间并通过调用函数来停止它们。开启步行并关闭。 - 非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不忘初心 2025-01-01 08:37:15

试试这个代码。请注意 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();

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();
红颜悴 2025-01-01 08:37:15

你想通过什么方式停止摇晃?摆动几次后是否会停止,或者用户应该单击按钮?

如果你想在几次后停止摆动,那么你应该使用计时器。在计时器事件上调用 wobble()。如果用户要停止摆动,则只需使用触发器 isStopped。如果 isStopped 设置为 true,则从 wobble() 返回。

By what way do you want to stop wobbling? Does wobbling stops after several times, or a user should click a button?

If you want to stop wobbling after several times, then you should use a timer. Call wobble() on timer event. If wobbling will be stoped by the user, than just use a trigger isStopped. If isStopped is setted to true, then return from wobble().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文