0
votes

Update:

I try access to (MySWF(movieClip)).id but I get a weird message error:

1120: Access of undefined property aslider

which is defined in MySWF.init. If I remove (MySWF(movieClip)).id program works perfect !

Isn't this weird especially as I have added addEventListener(Event.ADDED_TO_STAGE, init); in MySWF constructor to ensure that aslider is on the stage.

In main:

  private var idMySWF:int;
  public function loadMySWF(event:MouseEvent) {

  idMySWF = 1;
  var myLoader:Loader = new Loader();
  myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
  myLoader.load(new URLRequest("MySWF.swf"));           
  myZone.addChild(myLoader);

  }       

  public function loadComplete(evt:Event):void {

  var movieClip:MovieClip;
  var myLoader:Loader;
  myLoader = evt.currentTarget.loader as Loader;

  movieClip = MovieClip(loader.content);
  myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
  myLoader = null
  movieClip.addEventListener(CustomEvent.ON_CUSTOM_EVENT,OnCustomEvent);      
  (MySWF(movieClip)).id = idMySWF;

  }

In MySWF:

  private var _id:int;

  public function get id():int {
      return _id;
  }

  public function set id(__id:int):void {
      _id = __id;
  }

  public function MySWF() { 

    addEventListener(Event.ADDED_TO_STAGE, init);   

  }
  public function init( e:Event ):void {

    removeEventListener( Event.ADDED_TO_STAGE, init );      
    aslider.addEventListener(SliderEvent.CHANGE,OnSliderEventChange);

  }
3

3 Answers

1
votes

The line myZone.addChild(loader); should be myZone.addChild(myLoader); I guess and I'd put it before myLoader.load(new URLRequest("MySWF.swf")); just in case the loading is too quick!

1
votes

*** UPDATE *** Okay so I wasn't quite right in the original (below) answer. After having a look directly at the FLA it turns out it's an issue with using the document class combined with pre-defined instances of objects placed on the stage at design time. What happens is flash automatically generates/declares references to these instances at runtime, so the only way to really access them directly through the document class is to references them as so:

var myObject:Slider = this["sliderNameInFlashIDE"];

Here is a complete article about this issue and an alternative work-around that requires disabling this auto-declaration feature in the IDE.

http://blog.ickydime.com/2008/07/as3-notes-automatically-declare-stage.html

So basically as far as it applies to your code:

 public function OnCustomEvent(event:CustomEvent): void {

       this[\"labelValue\"].text = event.value + \"\";
       var cEvent: CustomEvent;
       cEvent = new CustomEvent(CustomEvent.ON_CUSTOM_EVENT);
       cEvent.value = event.value;
       this.dispatchEvent(cEvent);

 }

and

 public function init( e:Event ):void {

   removeEventListener( Event.ADDED_TO_STAGE, init );

   this.addEventListener(CustomEvent.ON_CUSTOM_EVENT,OnCustomEvent);
   this[\"aslider\"].addEventListener(SliderEvent.CHANGE,OnSliderChange);

 }

Solve the issue of having null object references.

Also if any of the code I've copied and pasted is too revealing about the nature of your project let me know and I can change the code to be more generic so the point is still made but the nature is obscured.

*** ORIGINAL ANSWER *** You're getting access undefined error because the object reference exists before the object does. The object is constructed when the display list for the parent is constructed. You need to add an event listener for Event.ADDED_TO_STAGE to the constructor of MySWF and add that event listener (SliderEvent) inside the callback to ADDED_TO_STAGE.

http://www.emanueleferonato.com/2009/12/03/understand-added_to_stage-event/

http://forums.adobe.com/message/3464172 (Specifically the post by Randy):

"The display object is "mostly" ready when the constructor code is hit. But there are times when it is safer to add an event listener for "Added to Stage" and in the handler everything will be initialized. For example, I have found it better to fetch get Flash Vars from the loaderInfo after this event fires."

0
votes

To begin with, your line:

idMySWF: 1;

Does not do what I think you expects it to, try using an equals sign instead:

idMySWF = 1;