【问题标题】:auto refresh swf movie iusing action script 3使用 actionscript 3 自动刷新 swf 电影
【发布时间】:2019-04-26 12:16:11
【问题描述】:

我正在闪存中创建记分板,正在从 REST 端点获取数据。能够正确读取一次数据,之后重复相同的数据无法读取更新数据。我试过计时器,有人能帮我吗

提前致谢

下面是我的代码

import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.display.Graphics;

    var white: Array = new Array();
    var black: Array = new Array();
    var red;
    var striker;

    var myTimer:Timer = new Timer(4000,100);
    myTimer.addEventListener(TimerEvent.TIMER, timerListener);
    function timerListener (e:TimerEvent):void{
    trace("Timer is Triggered");
        var urlLoader: URLLoader=null;

    // load the JSON data from the URL
    urlLoader = new URLLoader(new URLRequest("xxxxxxxxxxx"));
    urlLoader.addEventListener(Event.COMPLETE, loadData);

    // handle the load completion
    function loadData(e: Event):void {
        trace(e.target.data);

    }
    myTimer.start();
    stop();

【问题讨论】:

    标签: actionscript-3 flash flash-cs5


    【解决方案1】:

    虽然我不确定你的代码到底有什么问题,但有些东西我不会在项目中使用:

    1. 按时间重复向同一 URL 发出 HTTP 请求,而不是等待每个请求完成后再发送另一个请求。
    2. LoaderURLLoader 实例保存在本地函数变量中。通常,本地函数变量仅在函数执行时才存在,并且它们的内容(如果不以某种方式被应用程序范围保存)可能会在字面上 ANY 时刻被垃圾收集器销毁,而不考虑您的意图。 UPD: 有一个输入,GC 不会破坏 URLLoader,虽然它有未完成的请求要处理,但是,将不可破坏的实例悬挂在你无法到达的地方并不是好主意。
    3. 在其他函数中定义的函数。

    考虑到所有这些,以下应该可以正常工作:

    // Keep URLLoader instance within the scope.
    var L:URLLoader;
    
    // Issue the first request.
    loadNext();
    
    // This function issues the new request.
    function loadNext():void
    {
        var aReq:URLRequest = new URLRequest("xxxxxxx");
    
        L = new URLLoader(aReq);
        L.addEventListener(Event.COMPLETE, onData);
    
        // Error handling - a must have.
        L.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
        L.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError, false, 0, true);
    
        L.load();
    }
    
    // This function handles normal data processing.
    function onData(e:Event):void
    {
        // Sanity check. If event is triggered by anything
        // but the current URLLoader instance - do nothing.
        if (e.target != L) return;
    
        // Your data processing here.
        trace(L.data);
    
        // Clean-up the current request and schedule the next one.
        resetRequest();
        waitNext();
    }
    
    // This function handles all error events.
    function onError(e:Event):void
    {
        // Sanity check. If event is triggered by anyhting
        // but the current URLLoader instance - do nothing.
        if (e.target != L) return;
    
        // Report the error.
        trace("Oh, no:", e);
    
        // Clean-up the current request and schedule the next one.
        resetRequest();
        waitNext();
    }
    
    // This function destroys the current request.
    function resetRequest():void
    {
        // Sanity check. If there's no URLLoader instance - do nothing.
        if (!L) return;
    
        L.removeEventListener(Event.COMPLETE, onData);
        L = null;
    }
    
    // This function calls "loadNext" in 4 seconds.
    function waitNext():void
    {
        // Although the use of setTimeout counts as outdated,
        // it will still for our needs here.
        setTimeout(loadNext, 4000);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-23
      • 2011-10-22
      • 2011-10-27
      • 1970-01-01
      • 2015-08-21
      • 2010-12-03
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      相关资源
      最近更新 更多