1
votes

I have an Movie Clip in Flash that have subobject of button type which has subobject of input text and movie clips. Right after creation core Moveclip all subobject are set to null, when I expect them to be valid objects.

// hierarchy:

// core:MC_Core_design
    // button_1:B_Mybutton
        // text_name   // dynamic text with instance name
        // mc_icon     // movie clip with instance name

var core:MC_Core_design = new MC_Core_design();
addChild(core);
core.button_1.text_name.text = "hello world"; // error: text_name is null
core.button_1.mc_icon.visible = false; // error: mc_icon is null

MC_Core_design was created in Flash and exported to Actionscript. I've done this for button_1 class aswell. The code was written using Flex.

When I comment out both lines that result in error I get correct view of the core Movie clip with all subobject.

How can I set subobject properties right after object creation?

2

2 Answers

1
votes

You need to listen for the Event.INIT from the class when it is created. (If you are not embedding a symbol using the Embed metatag then Flash takes a few milliseconds to initialize the loaded movieclip). This does not seem to be a problem if the Flash IDE swf/swc does not contain any actionscript)

The issue is sometimes it can be really quick, so it fires the INIT event before you get a chance to attach the event listener to the object. so you can't just attach it after you instantiate the object.

A work around is to embed the swf as a byte array, then use the loader class to load the embedded bytes (This lets you set the event listener before calling load).

e.g.

[Embed(source="assets.swf", mimeType="application/octet-stream")]
private var assetBytes:Class;

private var clip:MovieClip;
private var loader:Loader;

public function LoadBytesExample()
{
    loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.INIT, onAssetLoaded);
    loader.loadBytes(new assetBytes());
}

 private function onAssetLoaded(e:Event):void
 {
     var loader:Loader = (e.currentTarget as LoaderInfo).loader;
     (e.currentTarget as LoaderInfo).removeEventListener(Event.INIT, onAssetLoaded);

      clip = loader.content as MovieClip;
      this.addChild(clip);

      clip.someTextField.text = "HELLO WORLD";
   }

Sorry for the formatting, just wrote that off the top of my head

And the syntax for embedding the symbol (You won't need to load this via a loader as the actionscript in the external swf/swc is stripped).

[Embed(source="assets.swf", symbol="somesymbol")]
private var assetSymbol:Class;

private var clip:MovieClip;

public function LoadSymbolExample()
{
   clip = new assetSymbol();
   clip.sometext.text = "Hello World";
}
0
votes

If I see it right, button_1:B_Mybutton is not yet initialized.

I mean something like : button_1:B_Mybutton = new B_Mybutton();

About the other two variables text_name & mc_icon as you describe if they have been initialized already (as you term them as instance names), Iguess they should not give you any problem.

Also I asssume that you are setting access modifiers to all as public.

If you still have problem... pls share how all the required variables are defined. Just the relevant part would be enough.