![]() ![]() ![]() this function will run once the load completes load the request, any code after this will run before the load completes. Loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleResponse) //this kind of error will happen if you're trying to load a local resource but have published as network only (or vice-versa) ![]() Loader.addEventListener(IOErrorEvent.IO_ERROR, handleResponse) //it's good practice to also listen for errors Private var loader: URLLoader = new URLLoader() //let's move it out to the class level so you can easily reference it from other methods (though this is purely optional) Here is a code example of how you would want to handle this based off of the code you supplied: Your data is null because in AS3 the load command is asynchronous - meaning it doesn't halt the rest of the application (locking it up) until the load completes, it moves on to the next line of code and dispatches an event once the load is complete. There isn't any nice way to do a synchronous load (like you can in some other languages) with AS3. Private function handleResponse(event:Event):void Loader.addEventListener(Event.COMPLETE, handleResponse) Var request: URLRequest = URLRequest("") I wrote this small example code, and wonder how I (the easiest way) can access the response in this get request. Then it seemed that I must create an event handling to actually access the response. ![]() I expected to find the response data in the URLLoader.data attribute, but after making the query this was still null in my case. I understand that when I want to do a network query I can create a URLRequest, and send this with a URLLoader. If you want your class to dispatch events, just extend EventDispatcher.ĭo I really need to have an event handler for URLLoader? I am new to actionscript, and I am following a tutorial by Joseph Devore. On a side note: there's absolutely no reason to extend from MovieClip. Otherwise the corresponding URLLoaders will not be garbage collected and will keep piling up in memory. There will be no performance hit for creating a bunch of event listeners as long as you remember to remove them when you're done with them. When an answer comes from the server - be it a COMPLETE or an IOERROR - you can remove the event listeners and remove the identified URLLoader (the one that matches event.currentTarget) from the Array. To solve this issue you would create a URLLoader (+ event listeners) for each call and put it in an Array to be able to identify it. Now, if you re-use the same URLLoader for both calls, there is no way you can know which COMPLETE event is going to correspond to which call. Is it a better practice to declare a method initListeners and call it everytime I call sendAndLoad? As a consequence, would it be a better practice that of removing the listeners inside the handleLoadError and handleLoadSuccesful methods? Are listeners cpu-intensive?ītw, I don't understand why this question "appears to be subjective", since we're talking about performances, not programming style.Īs your class is designed to be able to make multiple calls to the same webservice, I would definitely go for option 2. If you look at the URLTest constructor you'll find I initialize the listeners of an instance of URLLoader. Var scriptRequest:URLRequest = new URLRequest(" var scriptVars:URLVariables = new URLVariables() Public function handleLoadError($evt:IOErrorEvent):void Public function handleLoadSuccessful($evt:Event):void ScriptLoader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError) ScriptLoader.addEventListener(Event.COMPLETE, handleLoadSuccessful) AddEventListeners and best practices - actionscript-3 I'm using this simple code to retrieve a value from a PHP script: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |