I’m working on a flash project in which I have a swf with a bunch of art assets. However, they are not on the stage, there are simply in the library, and linked up to code I have written.
I have one class that I would like to instantiate, but I keep getting a reference error when it’s being constructed.
Here’s how the swf/fla Library is set up
[MovieClip]ButtonPopup, Linkage:com.packagename.ButtonPopup
[MovieClip]popup_btn, Linkage: none
popup_btn is a child of Popup, and aptly named _popup_btn (with a getter generated by Flash Builder called popup_btn) within the Flash CS5.5 IDE. Here’s the Popup class:
package com.packagename.ButtonPopup {
public class ButtonPopup extends Popup {
public Popup() {
addClickListeners(popup_btn);
}
protected function get popup_btn():MovieClip {
return this["_popup_btn"];
}
}
}
package com.packagename.Popup {
public class Popup extends MovieClip {
public Popup() {
}
protected function addClickListeners(mc:MovieClip) {
//add click listeners
}
}
}
My main swf looks like so:
public MainClass() {
var loader:Loader = new Loader();
loader.load(new URLRequest("assets.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadArtComplete);
}
function loadArtComplete (e:Event):void {
var popup_class:Class = e.target.applicationDomain.getDefinition("com.packagename::Popup");
var popup_mc:MovieClip = new popup_class() as MovieClip;
addChild(popup_mc);
}
Sadly, I get an error on the var popup_mc:MovieClip = new popup_class() as MovieClip line:
ReferenceError: Error #1056: Cannot create property _popup_btn on com.packagename.Popup
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at com.packageName.Popup() [file path]
at com.packageName.MainClass/loadArtComplete() [filepath]
If I leave the popup_btn unnamed in the Flash IDE, I don’t get an error, but then I can’t reference the MovieClip (for adding click listeners and the like), so that’s a no-go solution.
Does anyone have any ideas on what to do here? Can I not instantiate a “complex” MovieClip but instead have to instantiate each child MovieClip, one by one?
EDIT: After a bit more testing, it looks like it has to do with the fact that the problem class isn’t inheriting directly from MovieClip, but from another class (which is inheriting from MovieClip). I updated the source code to reflect that. I still don’t know if that’s the case (if not inheriting from MovieClip would cause a problem). But we’ll see.
Thanks,
-Esa
There is two solutions:
Set your class as dynamic :
then you won’t have flash error
OR
You need to create a public var named “_popup_btn” (typed with SimpleButton or MovieClip) in your ButtonPopup class, then Flash will sets this var with your popup_btn’s instance.