A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Access the stage from a child swf

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    6

    Access the stage from a child swf

    Hi guys,

    Been searching for some help on this one, can't seem to see any answers though... (bit of an impatient searcher....)

    I have a movie, which on the click of a button will load a child swf.
    The child swf has code which adds events to the stage object.
    the child also accesses properties such as stage.stageWidth

    When the child is ran on it's own all is fine, though when it loads within the parent movie i receive the error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Home()

    "Home()" is the name of the document class in the child SWF.

    Any ideas how i
    a) access the stage from a child swf
    b) add events to the stage from a child swf

    Cheers in advance

    D

  2. #2
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    you can do it one of two ways.

    1) Wait for the child swf to be added to the stage before trying to access it...

    myChildSwf.as
    Code:
    package
    {
       import flash.events.*;
       import flash.display.*;
       public class myChildSwf extends Sprite
       {
          public function myChildSwf()
          {
             addEventListener(Event.ADDED_TO_STAGE, init);
          }
          
          private function init(e:Event):void
          {
             removeEventListener(Event.ADDED_TO_STAGE, init);
             trace(Stage.stageWidth);
          }
       }
    }
    2) Pass the stage to the child swf when instantiating...

    Parent Swf
    Code:
    var myChild:myChildSwf = new myChildSwf(stage);
    myChildSwf.as
    Code:
    package
    {
       import flash.events.*;
       import flash.display.*;
       public class myChildSwf extends Sprite
       {
          public function myChildSwf(stg:*)
          {
             trace(stg.stageWidth);
          }
       }
    }
    Search first, asked questions later.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    ahh, great stuff, many thanks. Went with the first option for this particular swf - though thanks for the second option too - i'll be using this for a future child :-)

    regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center