开发者

ActionScript-3 timing issue / unwanted background multi-threading

I created a new class with private vars and public get properties.

When I create a new instance of the class, it loads text files content to the private vars - it probably takes a bit of time to load it.

After the new instance created, I try to get the value of the private var with the g开发者_如何学Cet property:

var item1:MyItem = new MyItem("0001");

trace(item1.ItemName);

Well, The output is blank.

The string that ItemName points to is not undefined, it contains data.

So, it's like a timing issue, and ActionScript is probably running the code using background multi-threading, so it's calling the trace command before it finished to run all methods in the MyItem c'tor (methods that load the text file data into the String var that ItemName points to).

Is there any way to force ActionScript avoid using this unwanted "background multi-threading", and run the code normally (by the order of the commands)?

I mean like "Don't run the 2nd command until you finished running the 1st one".

Thanks for any help..

Freddy


It's not multi-threading, loading a text file externally is just a completely asynchronous operation. Within your MyItem class, you need to have an Event.COMPLETE handler for that Loader. From there, there here is what I'd do:

Option 1: In MyItem's COMPLETE handler for the file load, set a flag. The class that uses the getter must check the flag via another getter to see if the data is there, and either use it (if it's there) or set up a listener to wait for it (if it's not). Once the data is loaded, you have immediate synchronous access to it (it's cached).

Option 2: An alternative to keeping the "isLoaded" flag within MyItem would be to have MyItem's Loader COMPLETE handler dispatch it's own TEXT_LOADED event. Then, the instance responsible for creating the MyItem instance would listen for that, and know not to ask for the contents of that text file until it was there.

Either approach will work. It is up to you to figure out which one makes sense. My first option potentially avoids an unnecessary listener if you don't expect to access the loaded data right away (i.e. you're loading the text at startup for use sometime later). The second approach makes sense if you do expect to make use of the text file's data the instant it loads.


Ideally what you'd want to do is implement the Observer Pattern:

  1. Add an event handler to your MyItem class for the Loaded event of whatever component/class you're using to load your XML.

  2. Add an event to your MyItem class that will bubble the Loaded event from Step 1 so that clients of your MyItem class can attach a handler to it. Fire this event inside the Loaded handler you created in Step 1.

  3. Attach an event handler to the event you created in Step 2 and place your trace code in that handler.


Your code is in fact running in a single thread, however the typical data loading classes in Actionscript are asynchronous. It is likely that the data has not loaded before you call trace. To be notified of when the data is loaded you should use an event listener on the loading object.

var loader:URLLoader = new URLLoader();
loader.addEventListener( Event.COMPLETE, onLoadComplete );
loader.load( new URLRequest( 'path_to_data.xml' ) );

function onLoadComplete( event:Event ):void 
{
     trace( loader.data );
}

The AS3 API docs have a more complete example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜