开发者

How can i use object from a lazy loaded swf file if the class definition needs to be changed?

开发者 https://www.devze.com 2023-03-14 05:26 出处:网络
I am converting all embed statements in my site with lazy loading. The code which was previously like this:

I am converting all embed statements in my site with lazy loading. The code which was previously like this:

[Embed(source="/newswf.swf", symbol="kungfu")]
public static var Kungfu:Class;

has now been converted to this form:

private var _loader:Loader = new Loader();
public static var开发者_Go百科 abcd:Class = null;
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgressHandler);
_loader.load(new URLRequest("newswf.swf"));



private function onLoadComplete(evt:*):void
{
    abcd = evt.target.applicationDomain.getDefinition("kungfu") as Class;
    dispatchEvent(new MyEvent(MyEvent.LOADING_DONE));
}

The functions which make use of abcd will be called on recieving MyEvent.LOADING_DONE event.

Now, my problem is, when a class makes use of symbol and has a class definition, I am not able to implement it using the above method because the constructor will be called immediately and won't listen to the onLoadComplete event listener.

[Embed(source="/newswf.swf", symbol="judo")]
public class Judo extends MovieClip
{
    public function Judo()
    {
        super(...);
    }
}

When i put the code in the constructor in a separate function and calling it in onLoadComplete method, I get an error because super method had initially been used in the constructor and it cannot be used outside of a constructor.

Can someone tell me a way to do lazy loading in this case?

Thanks in advance :)


I'm not sure if it is possible to extend the class definition after loading because I've never tried, but have you tried simply casting the loaded object and then not calling super() again? That is, inside the loader function type:

obj:Judo = Judo(LoaderInfo(e.target).content)

This article may be helpful: http://www.parorrey.com/blog/flash-development/as3-loading-external-swf-into-movieclip-using-loader-class-in-flash-actionscript3/


That said, I probably wouldn't structure the code in this way and just avoid the situation you're describing with a different structure. Like, one approach would be instead of making the loaded object into a Judo object I would initialize a separate Judo object and then pass it the loaded object. The old "has-a" vs. "is-a" distinction.

Another approach that accomplishes the same thing would be for the containing class to not do the loading and simply create a new Judo object, passing the filename into the constructor. Then the Judo object does the loading.

0

精彩评论

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