计时器不会造成延迟
Posted
技术标签:
【中文标题】计时器不会造成延迟【英文标题】:Timers not creating a delay 【发布时间】:2019-11-06 19:04:04 【问题描述】:我正在 Adobe Animate 中创建一个程序;其中一项功能是将 OSC 消息发送到 DMX 照明程序以改变房间内的照明。
标准更改按预期工作,但我遇到了“褪色”问题。我需要连续发送一系列 OSC 消息。
我现在拥有的是 Adobe Animate 通过一个独立的函数创建一系列计时器。我觉得我需要的是延迟功能,但我知道这在 AS3 中是不可能的。
function fadeFixtureData(fixture:int, rgbStart:Array, rgbEnd:Array, intervals:int):void
if (rgbStart.length != rgbEnd.length)
return void;
var rgbCalculated:Array = new Array();
for (var i = 0; i <= intervals; i++)
for (var j = 0; j < rgbStart.length; j++)
rgbCalculated[j] = ((((rgbEnd[j] - rgbStart[j])/intervals) * (i)) + rgbStart[j]);
delayedFunctionCall((i * 33), function(e:Event) sendFixtureData(fixture,rgbCalculated););
trace(i * 33);
trace(rgbCalculated);
function delayedFunctionCall(delay:int, func:Function)
var timer:Timer = new Timer(delay, 1);
timer.addEventListener(TimerEvent.TIMER, func);
timer.start();
该程序似乎正确地跟踪了所有内容,但结果是所有消息都同时发送。只有最后一条消息被转发到照明程序。
【问题讨论】:
您不能创建计划依赖于 for 循环计数器的匿名动态函数。抱歉,这不是 javascript 允许的。 【参考方案1】:您可以改用setTimeOut
。
var myTm = setTimeOut(delay, 1000); // in milliseconds
function delay(): void
// your delayed code
【讨论】:
【参考方案2】:我建议有两种方法。这两种方法都应该让你用新的方法打断任何淡入淡出。因此,在淡出进行到一半时,您可能会改变主意并想要再次淡出(例如,基于人类互动)。
为了实现这一点,在这些示例中,您只需根据需要再次触发 fadeTo(yourValue)
。
EnterFrame 方法:
public class Main extends Sprite
private var targetValue:Number;
private var currentValue:Number = 0;
private var increment:Number;
private static const MAX_VALUE:int = 255;
private static const FADE_TIME:Number = 5; // Seconds for a full fade from 0% to 100%.
public function Main()
increment = MAX_VALUE / (stage.frameRate * FADE_TIME); // Dynamically calculate based on app framerate.
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
// Initiate a fade.
fadeTo(1);
/**
* Initiates fade.
* @param percentage A value between 0 and 1. 0 being off, 1 being full on, 0.5 as an example, being 50% brightness.
*/
private function fadeTo(percentage:Number):void
if (percentage > 1) percentage = 1;
if (percentage < 0) percentage = 0;
targetValue = MAX_VALUE * percentage;
private function enterFrameHandler(e:Event):void
if (currentValue == targetValue) return; // No updates required.
if (currentValue < targetValue)
currentValue+= increment;
if (currentValue > targetValue) currentValue = targetValue;
else
currentValue-= increment;
if (currentValue < targetValue) currentValue = targetValue;
doRGBThing(currentValue);
private function doRGBThing(currentValue:Number):void
trace(int(currentValue)); // Replace this with your OSC related code.
Tween 方法(如 GreenSock 的 TweenLite):
public class MainTween extends Sprite
private var currentValueObj:Object = currentValue: 0;
private static const MAX_VALUE:int = 255;
private static const FADE_TIME:Number = 5; // Seconds for a full fade from 0% to 100%.
public function MainTween()
// Initiate a fade.
fadeTo(1);
/**
* Initiates fade.
* @param percentage A value between 0 and 1. 0 being off, 1 being full on, 0.5 as an example, being 50% brightness.
*/
private function fadeTo(percentage:Number):void
if (percentage > 1) percentage = 1;
if (percentage < 0) percentage = 0;
TweenLite.killTweensOf(currentValueObj);
TweenLite.to(currentValueObj as Object, FADE_TIME, currentValue: MAX_VALUE * percentage, onUpdate: doRGBThing);
private function doRGBThing():void
trace(currentValueObj.currentValue);
【讨论】:
以上是关于计时器不会造成延迟的主要内容,如果未能解决你的问题,请参考以下文章