在一个新层上,我将制作一个新的movieClip,它将成为帧的容器,并通过计时器控制它停止。
只需绘制一个仅具有填充颜色的框(无边框)。与您的舞台相同的宽度和高度,对齐到顶部和左侧(在修改->对齐下,勾选“对齐到舞台”)。它现在应该是舞台区域的完美契合,然后将其转换为movieClip(确保注册设置为顶部/左侧的黑点.. 不是居中的点)。
给它起你想要的名字,但给它 animMC 的实例名称(你可以稍后更改)。
现在选择整个动画帧范围,然后在右键菜单中选择“剪切帧”。双击animMC movieClip进入它的时间线,删除盒子形状并右键单击第一帧并“粘贴帧”。
现在在您的代码中,您可以像这样停止它:
package com.preview {
import flash.utils.Timer;
import flash.events.TimerEvent
import flash.utils.getTimer;
import flash.display.MovieClip;
public class looptimer extends MovieClip
{
var startTime:int = 0;
var allowedTime:int = 5000;
var loopTime:int = 0;
public function looptimer ()
{
var myTimer:Timer = new Timer(5000, 1); //count before doing function "timerListener" the 1 means do once else it will loop forever
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
function timerListener (e:TimerEvent):void
{
trace("Timer countdown finished");
animMC.stop(); //anim stops
}
}
}
}
=========================
编辑2:
嗨,这段代码将检查在动画结束时触发的事件。要让此类事件进入包含帧的 MC,并在最后一帧(以及空白层)添加此操作代码:(这将是
dispatchEvent(new Event("Banner_end)")); //end frame
这相当于获得“结束帧”。在下面的代码中,事件附加到此指令:
animMC.addEventListener("Banner_end", check_Time);
这意味着在到达结束帧时,会从时间线发送一个名为“横幅结束”的事件,这会依次执行 check_Time 函数和另一个函数(参见代码)。
现在,下面的代码将检查时间以及“结束帧”事件被调度时的计时器,并确定是否有足够的时间进行第二次循环或更多。有两件事会影响 MC 循环的次数。首先是时间限制,时间越长,您越适合该限制。其次是动画 MC 的长度,如果较短的方法可以在时间限制内适应更多循环。调整这两点。最简单的是时间限制,因为您只需更改一个数字(“allowed_playing_Time”)进行测试,但稍后当您无法控制限制时,您可以根据需要调整横幅(动画 MC)帧,即通过添加/删除帧。
package com.preview
{
import flash.utils.*;
import flash.events.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.utils.getTimer;
import flash.display.MovieClip;
import flash.events.Event;
public class looptimer extends MovieClip
{
var allowed_playing_Time:int = 10; //time limit in seconds
var play_Count:int = 0; //tracking number of times played
var can_play_Amount:int = 1; //total times can be played
var _startTime:Date;//Number;
var _endTime:Date;//Number;
//for time check using actual [computer] clock = more accurate than timer
var real_Time:Date;
var anim_start_time:int;
var anim_end_time:int;
var anim_play_length:int;
public function looptimer()
{
//starting the Banner
play_Banner_anim();
}
function play_Banner_anim():void
{
animMC.stop(); //set strating banner state as paused;
_startTime = real_Time = new Date();//Real Time value initial set here
trace("current time at START is: " + _startTime.minutes + " mins past and " + _startTime.seconds + " secs");
animMC.addEventListener("Banner_end", check_Time);
animMC.addEventListener("Banner_end", calculate_Replay);
anim_start_time = getTimer();
animMC.play();
}
function check_Time(e:Event):void
{
animMC.stop(); trace( "Banner has reached last frame");
animMC.removeEventListener("Banner_end", check_Time);
play_Count = play_Count + 1;
trace("Times banner has played / looped): " + play_Count);
_endTime = real_Time = new Date();//new update of Real Time value
trace("current time at END is: " + _endTime.minutes + " mins past and " + _endTime.seconds + " secs");
anim_end_time = getTimer();
anim_end_time = Math.ceil(anim_end_time);
anim_play_length = Math.round(Math.floor(anim_end_time - anim_start_time) / 1000);
anim_play_length = anim_play_length;
trace("checking if Real Time: ");
var date1:Date = _startTime;
var date2:Date = _endTime;
var elapsed1:Number = Date.UTC(date1.minutes,date1.seconds);
var elapsed2:Number = Date.UTC(date2.minutes,date2.seconds);
var playingTime:Number = Math.round( (elapsed2/1000) - (elapsed1/1000) );
trace ("time elasped (delta) is " + playingTime );
//check seconds elapsed in banner playback (i.e total play length)
trace("Banner total playing length was " + anim_play_length + " seconds");
//so how many times can banner play within allowed time limit?
var can_fit_Num:Number = allowed_playing_Time / anim_play_length;
trace("can fit this many plays in allowed time limit: " + can_fit_Num);
can_play_Amount = can_fit_Num;
trace("can_play_Amount is : " + can_play_Amount);
if (can_play_Amount <= 0)
{
trace("Warning: Banner Anim is either longer than allowed time limit or has a play length of zero");
}
}
function calculate_Replay(e:Event):void
{
trace("Calculating replay function :::::: ");
trace("Play/looped amount is: " + play_Count);
trace("Can play amount times is : " + can_play_Amount);
if (play_Count < can_play_Amount)
{
trace("anim_play_length is:" + anim_play_length);
replay_tings();
trace("replaying anim");
}
else
{
animMC.stop();
trace("reached replaying times (" + can_play_Amount + ") available within limit (" + allowed_playing_Time + " secs)");
if (can_play_Amount <= 0)
{ trace("Warning: Banner Anim is either longer than allowed time limit or has a play length of zero"); }
//do something else. Currently just stops since no time for replay
}
}
function replay_tings():void
{
animMC.addEventListener("Banner_end", calculate_Replay);
animMC.play();
play_Count = play_Count + 1;
trace("Play Count/loops: " + play_Count);
}
}
}
希望能帮助到你。我放了很多痕迹只是为了给你反馈。删除您认为不必要的任何内容,当然,如果您发现某些内容,您可以优化代码。