How to organize events in ActionScript 3

I’ve worked a lot with AS3 in recent weeks, and I’ve been confronted with a situation that Google couldn’t help. The essence of the problem is that before I run the function, I need to know that certain conditions are met, for example, loading my config.xml file and putting it in a class variable. Where I ran into difficulties, it is that URLLoader is asynchronous, so I cannot put the code after loading, the only thing I can do is put it in a lambda to the listener.

var conf:Object = new Object();

var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, geoLoader, false, 0, true);
ldr.load(new URLRequest("http://ipinfodb.com/ip_query.php"));

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

    conf.zip    = data.ZipPostalCode.toString();
    conf.city   = data.City.toString();
    conf.state  = data.RegionName.toString();
}

Or a little more succinctly, and I find it more understandable:

var conf:Object = new Object();

var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, function (e:Event){
    var data = new XML(e.target.data);

    conf.zip    = data.ZipPostalCode.toString();
    conf.city   = data.City.toString();
    conf.state  = data.RegionName.toString();
}, false, 0, true);
ldr.load(new URLRequest("http://ipinfodb.com/ip_query.php"));

, , ( , ) , ( , , ).

, : Flash ? , , , .

: , , , 5 , , 5 . , 5 . , : config.xml, , . , config.xml . , , , .

, Senocular Sequence.as, , , , . .

, !

+3
3

.

BulkLoader, , , , .

, , . , , , . , DataController. DataController - , , :

subscribe, notify

, , , XML, , :

- BulkLoader XML. XML, , imageXML :

DataController.instance.notify("ImageXMLLoaded", imageXML);

ImageHolder :

public function ImageHolder() {
    DataController.instance.subscribe("ImageXMLLoaded", _populate);
}

private function _populate($imageXML:XML):void {
    // do whaterver with your XML
}

DataController , :

private var _subscribers:Dictionary = new Dictionary(true);
private var _processed:Dictionary = new Dictionary(true);
private var _requests:Dictionary = new Dictionary(true);

:

public function subscribe($eventName:String, $subscriber:Function): void {
    var _funcArray:Array;
    var _func:Function;
    if (_processed[$eventName]) {
        $subscriber(_processed[$eventName]);
        return;
     }
     if (! _subscribers[$eventName]) {
        _subscribers[$eventName] = new Array();
     }
     _subscribers[$eventName].push($subscriber);
}        

:

public function notify($eventName:String, $data:*, $args:Object = null): void {
    var _cnt:Number;
    var _subscriberArray:Array;
    var _func:Function;

   _processed[$eventName] = $data;

   if (_subscribers[$eventName] != undefined) {
       _subscriberArray = _subscribers[$eventName].slice();
       _cnt = 0;
       while (_cnt < _subscriberArray.length) {
           _func = _subscriberArray[_cnt] as Function;

           if ($args) {
               _func($feedXML, $args);
           } else {
               _func($feedXML);
           }
           _cnt++;
        }
    }
    if (_requests[$eventName]) {
        _subscriberArray = _requests[$eventName].slice();
        delete _requests[$eventName];
        _cnt = 0;

        while (_cnt < _subscriberArray.length) {
        if (_subscriberArray[_cnt] != null) {
                _subscriberArray[_cnt]($data);
            }
            _cnt++;
        }
    }
}

:

public function request($eventName:String, $requestFunction:Function): void {
    var _requestArray:Array;
    var _request:Function;

    if (! _feeds[$eventName]) {
        if (! _requests[$eventName]) {
            _requests[$eventName] = new Array();
        }
        _requests[$eventName].push($feedFunction);
    } else {
        $requestFunction(_feeds[$eventName]);
    }
}

, , , $eventName _processed. , (_populate .) , _subscribers . , , DataController $eventName .

, , , - . .

_pcessed Dictionary.

, .

, - :

-, , .

public function startMyLoading():void {
    loadConfig(onConfigLoaded);
}

public function onConfigLoaded($xml:XML):void {
    DataController.instance.notify("configLoaded", $xml);
    loadWeatherContiditons(onWeatherConditionsLoaded);
}

public function onWeatherConditionsLoaded($xml:XML):void {
    DataController.instance.notify("weatherXMLLoaded", $xml);
}

-, , - :

public function WeatherDisplayView():void {
    DataController.instance.subscribe("weatherXMLLoaded", _populateWeatherPanel);
}

private function _populateWeatherPanel($xml:XML):void {
    // for each (var weatherNode:XML in $xml.children()) ...
    // use BulkLoader to load, and add Event.COMPLETE function for when it is finished.
}

private function _onBulkLoaderComplete($evt:Event):void {
    DataController.instance.broadcast("ImagesComplete"); // the broadcast function is one that I did not include above, but basically works the same as subscribe, notify, but without any data being passed.
}

, config.xml:

public function SomeClass():void {
    DataController.instance.subscribe("configLoaded", _handleConfigLoaded);
}

private function _handleConfigLoaded($xml:XML):void {
    // do something with config
}

, . config.xml weather.xml. - ... .. ..

5 BulkLoader , . .

+3

, , , , , , , , , - .

2 :

  • Event.COMPLETE, , .
  • conf.

conf, . , . , . , , ... conf , , , ..

,

+1

Maybe this is the situation for the survey? Create "Conditional" ones that have an enterframe receiver that calls them ovverridden isSatisfied (), where you put any logic that will tell you if there will be a runtime ()

public function onEnterFrame(event:Event):void
{
   if(isSatisfied())
   {
     execute();
     removeEventListener(Event.ENTER_FRAME,onEnterFrame);
   } 
}
+1
source

Source: https://habr.com/ru/post/1732210/


All Articles