所以我不确定你希望皱眉的脸多久出现一次。它可以在随机时间,或者在一定距离之后,或者您可以使用Timer 设置它以指定时间间隔运行。所以我将解释所有 3 个。
首先是随机时间。您需要为此解决方案导入 flash.utils.getTimer。我假设你希望你皱着眉头的脸保持皱眉脸超过 1 毫秒。如果是这样的话,那么我会这样做:
设置这个成员变量:
private var beginTime:Number;
然后在你运行你的第一个移动函数之前:
beginTime = getTimer();
在您的循环或移动函数中包含instancename.y += 10;
private function loop():void {
instancename.y += 10;
//get our delta time
var dt:Number = getTimer() - beginTime;
//set random variable 50% chance to change the frame
var random:int = Math.random() * 2;
//dt > 3000 just means 3 seconds have passed, you can lower that number to decrease the delay before we change frames for the "face" animation
if ( random > 0 && dt > 3000 ) {
beginTime = getTimer();
if ( instancename.currentFrameLabel == "neutral" ) {
instancename.gotoAndPlay("frowning");
}
else {
instancename.gotoAndStop("neutral");
}
}
}
这将随机更改帧,延迟 3000 毫秒或 3 秒(随意更改)。
现在是距离版本。所以这基本上只是说当我们从某个原点到达一定距离时,改变框架。但这取决于设置的几个变量:
//set the variable origin and a maxDistance
private var origin:Point = new Point( instancename.x, instancename.y );
private var maxDistance:int = 50;
//then in your loop or movement function
private function loop():void {
instancename.y += 10;
//when our distance is >= to our maxDistance, change the frame
if ( Point.distance( new Point( spr.x, spr.y ), origin ) >= maxDistance ) {
if ( instancename.currentFrameLabel == "neutral" ) {
instancename.gotoAndPlay("frowning");
}
else {
instancename.gotoAndStop("neutral");
}
//set the origin variable again
origin = new Point( instancename.x, instancename.y );
}
最后是计时器功能。使用TimerEvent.TIMER 的事件侦听器和要调用的函数设置一个计时器变量:
private var timer:Timer = new Timer(3000, 0);
然后在适用的情况下进行设置:
timer.addEventListener(TimerEvent.TIMER, changeFrame);
timer.start(); //to start your timer
然后在定时器函数中:
private function changeFrame( e:TimerEvent ):void {
if ( instancename.currentFrameLabel == "neutral" ) {
instancename.gotoAndPlay("frowning");
}
else {
instancename.gotoAndStop("neutral");
}
}
用完别忘了停止它:timer.stop();
这些是解决问题的几种方法。我应该注意,第二种解决方案(距离之一)可以通过多种不同的方式进行优化,这只是其中一种方式。
希望这会有所帮助,祝你好运!