【问题标题】:Tweening with actionscript 3使用 actionscript 3 补间
【发布时间】:2013-04-02 14:50:32
【问题描述】:

我一直在研究这个。在这种情况下,我有一个名为“mcBall”的影片剪辑对象。我用补间设置了两个按钮“btnLeft”和“btnRight”,这样 mcBall 就可以在两点之间顺畅地缓和。 工作正常,但问题在于这两个按钮仍然处于活动状态,如果用户单击任一按钮,当然球会像预期的那样回到起点。 我的问题是……在“mcBall”对象移动时停用按钮的最佳方法是什么。最好为按钮使用 removeEventListener 然后再次添加它。使用“If (mcBall.x = >=81 ||

任何帮助将不胜感激。谢谢

使用 Flash cs3

在这段代码中,我设法关闭了一个按钮,以便在球移动时它保持非活动状态,而另一个按钮仍处于活动状态。我不确定 removeEventListener 的位置。

import fl.transitions.Tween;
import fl.transitions.easing.*;

function moveBallRight(evt:MouseEvent):void {
    var moveBall:Tween=new Tween(mcBall,"x",Regular.easeOut,80,470,4,true);
    btnRight.removeEventListener(MouseEvent.CLICK,moveBallRight);
    btnLeft.addEventListener(MouseEvent.CLICK,moveBallLeft);

}
btnRight.addEventListener(MouseEvent.CLICK,moveBallRight);


function moveBallLeft(evt:MouseEvent) {
    var moveBall:Tween=new Tween(mcBall,"x",Regular.easeOut,470,80,4,true);
    btnRight.addEventListener(MouseEvent.CLICK,moveBallRight);
    btnLeft.removeEventListener(MouseEvent.CLICK,moveBallLeft);


}

btnLeft.addEventListener(MouseEvent.CLICK,moveBallLeft);

【问题讨论】:

    标签: actionscript-3 animation flash tween


    【解决方案1】:

    我建议不要使用来自 fl.transitions.Tween 的原生 Tween 类。它不是很好。行业标准是TweenMax from greensock

    使用 TweenMax 可以非常轻松地响应补间结束事件。您只需在补间中添加一个onComplete:myhandlerfunction。 上面代码中的一个示例如下所示: 而不是

     var moveBall:Tween=new Tween(mcBall,"x",Regular.easeOut,80,470,4,true);
    

    你会:

    TweenMax.to(mcBall, 4, {x:470, ease:Expo.easeOut, onComplete:onBallMovedLeftComplete};
    

    希望对您有所帮助。我希望你永远不必再使用那些原生补间类。他们是坑。

    【讨论】:

    • 感谢 Plastic 我现在就去 tweenMax 试试看!
    • 值得注意的是,TweenMax 有点臃肿。除非您需要特定的 TweenMax 功能,否则您应该使用 TweenLite(包含在同一个包中,TweenMax 实际上扩展了 TweenLite)。 TweenLite 产生的动画稍微好一点,因为发生的动画少了一点。
    • 太棒了!我想我将从“TweenLite”开始。看起来“OverwrightManager”模式是我问题的答案。我读过一本名为“Real World Flash Game Dev”的旧书。关于绿袜。那时我应该捡起来的。谢谢你们摇滚!
    • 如果您认为答案回答了问题,请随时点击接受 :-)
    【解决方案2】:

    我同意您应该按照其他答案的建议使用 TweenLite 或 TweenMax。

    根据我收集到的信息,问题是为按钮激活/停用事件侦听器的最佳方法。

    我想说最好的方法是拥有一个添加按钮侦听器的函数和另一个删除它们的函数。

    然后,每当您调用补间时,您首先调用删除函数,然后再执行补间。

    然后在完成后,您调用该函数再次添加它们。您可以在 Tweenlite 中使用 onComplete 参数来指定添加按钮侦听器的函数。

    例如:

    function moveBallRight(evt:MouseEvent):void
    {
        removeButtonListeners();
        TweenLite.to(mcBall, 4, {x:470, ease:Expo.easeOut, onComplete:addButtonListeners};
    }
    
    function moveBallLeft(evt:MouseEvent):void
    {
        removeButtonListeners();
        // do your tween
        TweenLite.to(mcBall, 4, {x:80, ease:Expo.easeOut, onComplete:addButtonListeners};
    }
    
    function addButtonListeners():void
    {
       // add both listeners here
    }
    
    function removebuttonListeners():void
    {
      // remove both listeners here
    }
    

    此外,您显然还希望在程序开始时调用 addButtonListeners,以便在程序运行时侦听器最初处于活动状态。

    【讨论】:

      【解决方案3】:

      我个人建议使用Actuate tween 库。与 TweenLite/Max 不同,它是完全开源的,具有大部分功能,而且速度更快,没有专业/付费版本。

      我也喜欢 Actuate 的界面,比 TweenLite 好得多。它非常相似,人们很容易开始使用它,但我喜欢以更明确的方式添加补间修饰符。

      简单示例:

      Actuate.tween(mySprite, 1, { alpha:1 });
      

      那么你是不是想指定一个缓动方程,把它链在最后:

      Actuate.tween(mySprite, 1, { alpha:1 }).ease(Quad.easeOut);
      

      也想要延迟?将其添加到链中:

      Actuate.tween(mySprite, 1, { alpha:1 }).delay(1).ease(Quad.easeOut);
      

      当然你也可以调用 onComplete 函数,即使有参数:

      Actuate.tween(mySprite, 1, { alpha:1 }).onComplete(trace, 'Tween finished');
      

      查看上面链接的 Actuate Google 代码页面以获取带有示例的完整方法列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-25
        • 1970-01-01
        • 2013-12-27
        相关资源
        最近更新 更多