计时器不产生延迟

时间:2019-06-25 01:12:38

标签: actionscript-3 actionscript air

我正在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();
}

该程序似乎在正确跟踪所有内容,但结果是所有消息都在同一时间发送。只有最后一条消息会中继到照明程序。

2 个答案:

答案 0 :(得分:0)

您可以改用setTimeOut

var myTm = setTimeOut(delay, 1000); // in milliseconds

function delay(): void {
    // your delayed code
}

答案 1 :(得分:0)

我有两种建议。两种方法都可以让您用新的方法中断任何淡入淡出。因此,在淡出的一半时,您可能会改变主意并想要再次淡出(例如,如果这是基于人类的交互作用的话)。

要实现这一点,在这些示例中,您只需要根据需要再次触发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);
    }
}