Actionscript 3 AS3_手动和自动按钮事件处理程序故障排除
我有一个项目,有30-40帧与movieclips在每一帧。将每一帧视为幻灯片。舞台底部的按钮控制这些幻灯片的功能,如播放、暂停、下一步、上一步、加速、减速和刷新。然而,让我头疼的两个按钮是手动模式和自动模式按钮。他们的名字正是他们的声音,手册应该停止后,每个mc在每个帧播放。自动应按顺序播放每一帧。我现在设置它的方式是,每个mc都会触发一个事件分派,即finished和stopped。根据上次单击的按钮,后台将侦听事件,并暂停4秒setInterval和gotoNextFrame,或在该帧上停止,直到用户单击next。我尝试了一个switch案例,但它不允许在每个案例中使用eventlisteners错误1009。这是我到目前为止的按钮代码 我是个新手,所以请温柔一点。我会上传flv,如果它能帮上忙的话。多谢各位Actionscript 3 AS3_手动和自动按钮事件处理程序故障排除,actionscript-3,Actionscript 3,我有一个项目,有30-40帧与movieclips在每一帧。将每一帧视为幻灯片。舞台底部的按钮控制这些幻灯片的功能,如播放、暂停、下一步、上一步、加速、减速和刷新。然而,让我头疼的两个按钮是手动模式和自动模式按钮。他们的名字正是他们的声音,手册应该停止后,每个mc在每个帧播放。自动应按顺序播放每一帧。我现在设置它的方式是,每个mc都会触发一个事件分派,即finished和stopped。根据上次单击的按钮,后台将侦听事件,并暂停4秒setInterval和gotoNextFrame,或在该帧上停
//Manual button actions
function manual_onClick(event:MouseEvent)
{
manual_btn.visible = false;
auto_btn.visible = true;
gotoAndStop(currentFrame);
stage.removeEventListener("finished", mcFinished);
stage.addEventListener("stopped",stopmc,false,0);
function stopmc(e:Event):void
{
trace("mc stop");
stage.removeEventListener("stopped",stopmc);
}
}
//Auto button actions
function auto_onClick(event:MouseEvent)
{
gotoAndStop(currentFrame + 1);
manual_btn.visible = true;
auto_btn.visible = false;
stage.addEventListener("finished", mcFinished2,false,1);
function mcFinished2(e:Event):void
{
var ID2 = setInterval(goNextFrame2,3000);
trace("mc complete");
function goNextFrame2()
{
gotoAndStop( currentFrame + 1 );
clearInterval( ID2 );
stage.removeEventListener("finished", mcFinished2);
}
}
}
以及来自每个mc的dispatchEvent
stop();
dispatchEvent(new Event("finished", true));
dispatchEvent(new Event("stopped", true));
再次感谢!
斯科特
这是交换机案例尝试
function onBtnClicked(evt:MouseEvent):void
{
var theBtn:DisplayObject = evt.currentTarget as DisplayObject;
var lastBtn:DisplayObject;
if (lastBtn)
{
lastBtn.addEventListener(MouseEvent.CLICK, onBtnClicked);
}
lastBtn = theBtn;
switch (theBtn)
{
case auto_btn :
//button one actions;
gotoAndStop(currentFrame + 1);
manual_btn.visible = true;
auto_btn.visible = false;
trace("auto button clicked");
stage.addEventListener("finished", mcFinished);
function mcFinished(e:Event):void
{
var ID = new setInterval(goNextFrame,3000);
trace("mc complete");
function goNextFrame()
{
gotoAndStop( currentFrame + 1 );
clearInterval( ID );
stage.removeEventListener("finished", mcFinished);
stage.addEventListener("finished", mcFinished);
}
}
break;
case manual_btn :
manual_btn.visible = false;
auto_btn.visible = true;
trace("manual button clicked");
stage.removeEventListener("finished", mcFinished);
stage.addEventListener("stopped",stopmc,false,1);
function stopmc(e:Event):void
{
trace("mc stop");
stage.addEventListener("stopped",stopmc);
stage.removeEventListener("stopped",stopmc);
}
break;
}
}
以下是一些建议 首先,您不需要在每个剪辑结束时完成/停止两个单独的事件,因为它们具有完全相同的功能。我建议只做一个事件,并使用内置事件,如event.COMPLETE。因此,单个剪辑的最后一帧将如下所示:
stop();
dispatchEvent(new Event(Event.COMPLETE, true));
在您个人剪辑的第一帧中,我将添加以下内容:
//use these two listeners so that the clip is only listening when it's being displayed on the timeline.
this.addEventListener(Event.ADDED_TO_STAGE,init,false,0,true);
this.addEventListener(Event.REMOVED_FROM_STAGE,unload,false,0,true);
function init(e:Event){
stage.addEventListener("Play",playMe,false,0,true);
stage.addEventListener("Pause",pauseMe,false,0,true);
}
function unload(e:Event){
stage.removeEventListener("Play",playMe,false);
stage.removeEventListener("Pause",pauseMe,false);
}
function playMe(e:Event){
play();
}
function pauseMe(e:Event){
stop();
}
下面是我将如何处理您代码的其余部分:
stop(); //stop the main timeline
manual_btn.addEventListener(MouseEvent.CLICK, onBtnClicked);
auto_btn.addEventListener(MouseEvent.CLICK, onBtnClicked);
var nextTimer:Timer = new Timer(4000,1); //a timer to wait 4 seconds before auto next.
nextTimer.addEventListener(TimerEvent.TIMER, nextSlide); //what the timer should do when it's done
var isAuto:Boolean = true; //a variable that holds the state of automatically continuing to the next slide. set it's default to either true or false
this.addEventListener(Event.COMPLETE,slideComplete); //listens for any slide complete event
//this function will get run whenever one of your slides is complete.
function slideComplete(e:Event = null):void {
if(isAuto){
nextTimer.reset();
nextTimer.start(); //start the timer
}
}
function onBtnClicked(evt:MouseEvent):void {
manual_btn.visible = (evt.currentTarget == manual_btn);
auto_btn.visible = (evt.currentTarget == auto_btn);
isAuto = auto_btn.visible;
}
function nextSlide(e:Event = null):void { //default e to null so you can call this function directly nextSlide(); - have your next button click call this function too
nextTimer.reset(); //stop/reset the timer
nextFrame(); //tell the main timeline to go to the next frame
}
function prevSlide(e:Event = null):void {
nextTimer.reset(); //reset the timer in case it's running
prevFrame();
}
function pause(e:Event = null):void {
nextTimer.reset():
stage.dispatchEvent(new Event("Pause")); //have your individual clips listen for this event and stop
}
function playSlide(e:Event = null):void {
nextTimer.reset();
stage.dispatchEvent(new Event("Play")); //have your individual clips listen for this event and play;
}
您希望在所有按钮点击时重置计时器的原因是,如果在自动下一个按钮等待触发时,您在4秒钟的窗口中点击上一个或下一个按钮。示例:打开“自动下一步”,幻灯片结束后1秒,您点击“上一步”按钮-它将移动到上一张幻灯片,3秒后计时器将启动,它将移动到下一帧。什么东西对您不起作用?你提到了一个switch语句,但我在你的代码中没有看到。上面的代码只工作一个周期。也就是说,在代码开始侦听这两个dispatchevents之前,我只能单击manual按钮和auto按钮一次,即使每个按钮都应该删除上一个按钮。开关箱贴在上面。它收到此错误,但仍将发布。TypeError:Error 2007:参数侦听器必须为非null。flash.events::EventDispatcher/addEventListener at flash.display::Stage/addEventListener上面的代码在哪里?在时间线的第一个帧上保存所有幻灯片帧?所有按钮的代码都在第一个帧上。所有幻灯片都在主时间线的第2-35帧中。调度事件放置在帧2-35上的mc内。谢谢谢谢你的回复!不过,我有几点意见。首先,我更喜欢您的方法,但我忘了提到我的主要目标是在第一帧上保留尽可能多的代码。原因是,这将是一个模板,为其他动画,我必须修改。我发布了带有旧switchcase的.fla,可以在[link]下载。有些电影的口吻也很长,因为它们是很久以前拍的。如果有机会,请看一看。另外,我无法让自动、手动或播放按钮与您的代码一起工作。swf的播放每帧延迟4秒,但我希望能够在自动模式和手动模式之间切换,自动模式延迟播放每张幻灯片,手动模式仅允许使用屏幕底部的“上一页/下一页”和编号按钮查看幻灯片。如果你下载,你会看到我在说什么。非常感谢您的帮助。仅供参考底部的编号按钮不起作用,因为我尚未完成阵列。仍然是一个缓慢的neuband为了保密,我不得不删除第10-30帧,但这里是这个[链接]的整体想法的jist,它不起作用,因为你没有在每张幻灯片的末尾发送一个完整的事件。将幻灯片更改为dispatch Event.COMPLETE,或将第一帧上的侦听器更改为侦听finished而不是Event.COMPLETE