派发变更值并开始补间

时间:2019-07-08 14:17:02

标签: actionscript-3 flash

我尝试制作简单的Scorebug。它来自外部XML文件。

我想在分数改变时开始播放动画,但是我没有找到事件监听器或调度程序来改变值。

var myTimer:Timer = new Timer(100);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();

function timerListener (e:TimerEvent):void 
{
    var myXMLLoader:URLLoader = new URLLoader();
    myXMLLoader.load(new URLRequest("Xml.xml"));
    myXMLLoader.addEventListener(Event.COMPLETE, processXML);

    function processXML (e:Event):void
    {
        var myXML:XML = new XML(e.target.data);

        ShotClock.text = myXML.timestamp;
        GameClock.text = myXML.Clock;
        HS.HomeScore.text = myXML.HomeScore;
        AS.AwayScore.text = myXML.AwayScore;
        Period.text = myXML.Period;
        AwayTeam.text = myXML.AwayName;
        HomeTeam.text = myXML.HomeName;
    }

    if ( (myXML.HomeScore).CHANGE ) 
    { var myTween:Tween = new Tween(HS.HomeScore, "alpha", Strong.easeIn, 0, 1, 1, true); }

}

1 个答案:

答案 0 :(得分:2)

您需要更改加载逻辑。而不是开始定时加载(无论如何都不会精确到100毫秒),可能会滞后,可能失败,可能会按您发出的顺序发送,等等,您需要一个单线程异步循环来执行以下操作: :

  1. 开始加载数据。
  2. (异步暂停)
  3. 处理加载的数据。
  4. 从加载的文本中获取XML数据。
  5. 解析XML属性。
  6. 检查分数变量是否已更改,并触发您想要的事情
  7. 等待100毫秒。
  8. (异步暂停)
  9. 转到步骤№1。

类似的东西:

var theLoader:URLLoader;
var theScore:Number = 0;
var theTimer:Timer;

// Start the tracking routine.
trackNext();

function trackNext():void
{
    theLoader = new URLLoader;
    theLoader.addEventListener(Event.COMPLETE, onXML);

    // Error tracking is a must or the logic flow
    // might just stop when you least expect it to.
    theLoader.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);

    theLoader.load(new URLRequest("xml.xml"));
}

function onXML(e:Event):void
{
    // Sanity check. There should be only one valid URLLoader instance.
    if (e.target != theLoader)
    {
        return;
    }

    var X:XML;

    try
    {
        X = new XML(theLoader.data);
    }
    catch (fail:Error)
    {
        // Processing the case where data is loaded successfully,
        // but it is not a valid XML String.
        onError(e);
        return;
    }

    // Here's the place for your code
    // that extracts data from XML.
    // ...

    // Get the new score value.
    var aScore:Number = X.HomeScore;

    // Compare it to previous value.
    if (aScore != theScore)
    {
        // ATTENTION!!! THIS IS THE PLACE YOU ARE LOOKING FOR!!!
        // Trigger things if the new score is any different.
        // ...

        // Keep the new score value.
        theScore = aScore;
    }

    finishLoading();
}

// This method just ignores an error, think of it
// as of blank to process the exceptional cases.
function onError(e:Event):void
{
    // Sanity check. There should be only one valid URLLoader instance.
    if (e.target != theLoader)
    {
        return;
    }

    finishLoading();
}

// Call it finishLoading(true) to stop the tracking routine.
function finishLoading(thenstop:Boolean = false):void
{
    // Dispose of the URLLoader instance if any.
    if (theLoader)
    {
        var aLoader:URLLoader = theLoader;

        theLoader = null;

        aLoader.removeEventListener(Event.COMPLETE, onXML);

        try
        {
            aLoader.close();
        }
        catch (fail:Error)
        {
            // Do nothing about it.
        }
    }

    finishTimer();

    if (thenstop)
    {
        return;
    }

    // Wait 100 ms to give Flash Player a breather
    // before starting to load the file once more.
    theTimer = new Timer(100, 1);
    theTimer.addEventListener(TimerEvent.TIMER, onTime);
    theTimer.start();
}

function finishTimer():void
{
    // Dispose of the Timer instance if any.
    if (theTimer)
    {
        theTimer.removeEventListener(TimerEvent.TIMER, onTime);
        theTimer.stop();
        theTimer = null;
    }
}

function onTime(e:TimerEvent):void
{
    // Now go for the next iteration.
    finishTimer();
    trackNext();
}