AS3按钮可在完成播放后停止Movieclip [英] AS3 Button to stop Movieclip after its finished playing

查看:122
本文介绍了AS3按钮可在完成播放后停止Movieclip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我是AS3和Flash的初学者,我设法将这段代码放在一起进行动画处理.一个名为start_btn的Button应该启动和停止一个名为main_mc的动画片段.在第一次单击按钮时,应该播放Movieclip(确实如此),但是在第二次单击时,电影会在其动画的中间停止(这是我不希望的). 我的问题是,当您再次单击按钮时,如何使Movieclip完成播放动画然后停在最后一帧?

Ok, so I'm a beginner at AS3 and Flash and I managed to put this code together for an animation. A Button called start_btn is supposed to start and stop a movieclip called main_mc. On the first click of the Button, the Movieclip is supposed to play (which it does), however on the second click, the movie stops in the middle of its animation (which I don't want). My question is, when you click the Button a second time, how can i get the Movieclip to finish playing its animation then stop on the last frame?

我考虑过使用if (main_mc.currentFrame == main_mc.totalFrames); {main_mc.stop();,但是Movieclip仍不会在最后一帧停止. Movieclip本身在最后一帧上还具有gotoAndPlay(2);命令,以便在第二次单击Button之前重复动画.

I thought about using if (main_mc.currentFrame == main_mc.totalFrames); {main_mc.stop(); but the Movieclip still does not stop on the last frame. The Movieclip itself also has a gotoAndPlay(2); command on the last frame so that the animation repeats before the Button is clicked a second time.

这是我的代码:

`start_btn.addEventListener(MouseEvent.CLICK,mainaniS);

`start_btn.addEventListener(MouseEvent.CLICK, mainaniS);

function mainaniS(event:MouseEvent):void 
{
    main_mc.play();
    start_btn.removeEventListener(MouseEvent.CLICK, mainaniS);
    start_btn.addEventListener(MouseEvent.CLICK, mainaniSt);
    }
function mainaniSt(event:MouseEvent):void
{
    if (main_mc.currentFrame == main_mc.totalFrames);
        {main_mc.stop();}
    start_btn.removeEventListener(MouseEvent.CLICK, mainaniSt);
    start_btn.addEventListener(MouseEvent.CLICK, mainaniS);
    }`

推荐答案

我打算提供一种快速而肮脏的解决方案,但我决定尝试解释当前实施中的一些问题,并尝试进行重构和解释.和更好的一个.不幸的是,我现在无法访问Flash,因此代码未经测试.

I was going to provide a quick and dirty solution, but decided instead to try and explain a few of the issues with your current implementation and attempt to refactor and explain and better one. Unfortunately I don't have access to Flash right now, so the code is untested.

您经常添加和删除事件侦听器,这通常是一个坏主意.相反,由于您使用单个按钮来执行多个功能,因此有必要在单独的变量中跟踪按钮状态.在这种情况下,为动画片段当前是否为playing的布尔值.

You're adding and removing event listeners often, which is generally a bad idea. Instead, since you're using a single button to perform multiple functions it would make sense to track the button state in a separate variable. In this case, a boolean for whether or not the movieclip is currently playing.

var playing:Boolean;

现在,我们可以将mainaniSmainaniSt组合为一个,并根据是否播放动画片段执行不同的动作,只需将一个事件监听器保持在按钮上即可.我还随意命名该方法一些更有意义的东西:

Now we can combine the mainaniS and mainaniSt into one and perform a different action based on whether or not the movieclip is playing, and just keep the one eventlistener on the button. I've also taken the liberty of naming the method something more meaningful:

start_btn.addEventListener(MouseEvent.CLICK, onStartClick);

function onStartClick(event:MouseEvent):void 
{
    if(playing) {
        playing = false;
    }
    else {
        playing = true;
        main_mc.play();
    }
}

您可能想知道为什么我们在第一段中不调用main_mc.stop():原因是您不想在单击按钮后立即停止动画片段,而是在结束动画片段播放后该按钮已被单击.因此,我们只是将playing设置为false表示我们希望它稍后停止.

You may be wondering why we don't call main_mc.stop() in the first block: the reason is that you don't want to stop the movieclip as soon as you click the button, but after the movieclip has finished playing if the button has been clicked. Therefore, we just set playing to false to indicate that we want it to stop later.

最后,我们需要确保动画片段在完成后停止,但前提是playingfalse.为此,我们向movieclip添加一个侦听器,该侦听器每帧都被调用一次,并检查playing是否为false,以及是否在最后一帧中.请注意,最后一帧实际上是totalFrames - 1:这是因为帧号从零开始而不是一(即,如果totalFrames为3,则帧号将为0、1、2).

Finally, we need to make sure the movieclip stops upon completion, but only if playing is false. To do this we add a listener to movieclip that is called every frame, and checks whether playing is false, and if it's on the last frame. Note that the last frame is actually totalFrames - 1: this is because the frame numbers start from zero rather than one (i.e. if totalFrames is 3, the frame numbers will be 0, 1, 2).

main_mc.addEventListener(Event.ENTER_FRAME, animate);

function animate(event:Event):void {
    if(!playing && main_mc.currentFrame == main_mc.totalFrames - 1) {
        main_mc.stop();
    }
}

所有重构的代码在一起:

All the refactored code together:

var playing:Boolean;
start_btn.addEventListener(MouseEvent.CLICK, onStartClick);
main_mc.addEventListener(Event.ENTER_FRAME, animate);

function onStartClick(event:MouseEvent):void 
{
    if(playing) {
        playing = false;
    }
    else {
        playing = true;
        main_mc.play();
    }
}

function animate(event:Event):void {
    if(!playing && main_mc.currentFrame == main_mc.totalFrames - 1) {
        main_mc.stop();
    }
}

这篇关于AS3按钮可在完成播放后停止Movieclip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆