dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: URGENT: access a main SWF var from nested SWF?

  1. #1
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341

    URGENT: access a main SWF var from nested SWF?

    Hi,

    There is a way to access a main SWF var from a nested SW?
    I have wasted a lot of time already and did get nothing. I am with a project stucked and a blowed timeframe, about to give up the job just because this tiny problem.

    Main SWF:

    Code:
    import flash.events.Event;
    
    stop();
    
    var Language:String = "spanish";
    var request:URLRequest = new URLRequest("teste.swf");
    var loader:Loader = new Loader()
    
    loader.contentLoaderInfo.addEventListener(flash.events.Event.COMPLETE, finished_loading);
    loader.load(request);
    
    function finished_loading (e:Event)
    {
    	var externalMovie = MovieClip(loader.content);
    	stage.addChild(externalMovie);
    	mcPreloader.visible = false;
    	externalMovie.gotoAndPlay(2);
    }
    Nested SWF:
    Code:
    trace (MovieClip(this.parent.parent).Language);
    I get always the same error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at teste_fla::MainTimeline/frame1()

    Thanks for helping!
    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  2. #2
    flash absorption vessel
    Join Date
    Jul 2009
    Posts
    31
    Your code to get at it is right, you just need to declare Language as 'public'. Without a declaration it defaults to 'protected', which would not be accessible outside the class.

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Please use [php] or [code] tags, and mark your threads resolved 8)

  4. #4
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341
    Attune,

    I WOULD do that if AS3 allow me to use public outside a package. Since I am coding directly in FLA, I cannot do that.

    Any clues, please??
    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  5. #5
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341
    Quote Originally Posted by neznein9 View Post
    After try the example in the link above, that just changes my code to:

    Code:
    trace (MovieClip(parent).Language);
    Now, I got a new error:

    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@1ba04b51 to flash.display.MovieClip.

    Seems that it is a casting problem. How should I solve that?
    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The link neznein9 posted is what you need to read. Also, since you added the loader to the stage rather than to the main movieclip, you cannot get to the main movie timeline via any number of parents. Change "stage.addChild" to just "addChild".

  7. #7
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341
    5Tons, I tried that and still not working. Read above! New error now!

    PS: Sorry all for being so laconic in my texts, I am suffering of a real bad internet connection today too (to complete my disgrace) so I cannot waste too much online time!!!

    I am seated into my car in the backyard with notebook over my lap, because it seems to be the only place where I can get a small and weak web signal on 3G.

    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your new error is caused by using stage.addchild instead of addchild, as I said above.

    What your structure should have looked like:
    Code:
    stage
    |
     - root (with language var)
        |
         - child
    But what you actually did was:
    Code:
    stage
    |
     - child
    |
     - root (with language var)
    So you can see that the root is not a display ancestor as you have it set up, and you'll never get to the variable you need by accessing parents. Change the structure to the first one.

  9. #9
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341
    5Tons,

    I have changed it to child (removed the Stage), and the problem persists:

    Main:

    Code:
    import flash.events.Event;
    
    stop();
    
    var Language:String = "spanish";
    var request:URLRequest = new URLRequest("teste.swf");
    var loader:Loader = new Loader()
    
    loader.contentLoaderInfo.addEventListener(flash.events.Event.COMPLETE, finished_loading);
    loader.load(request);
    
    function finished_loading (e:Event)
    {
    	var externalMovie = MovieClip(loader.content);
    	addChild(externalMovie);
    	mcPreloader.visible = false;
    	externalMovie.gotoAndPlay(2);
    }
    Loaded:

    Code:
    trace (MovieClip(parent).Language);
    Output:

    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Loader@21974041 to flash.display.MovieClip.

    I am REALLY frustrated...

    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your child movie must be calling that before it has been added to the display list, or you added the loader rather than the actual content as you showed above.

    In your child movie, make that stuff contingent on the ADDED_TO_STAGE event.

    Code:
    addEventListener(Event.ADDED_TO_STAGE, init);
    
    function init(event:Event):void{
      trace(MovieClip(parent).Language);
      //other initialization code.
    }
    Ideally, your two swfs would not be communicating directly like that. You'd set up events and interfaces between them so that everything is properly abstracted and typechecked.

  11. #11
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    341
    5Tons,

    WTF, it worked now!

    What doesnt make sense for me is why I should use an event to control that. Since the second movie will be loaded just after the first movie ran (and therefore the Language var was been initialized), wouldnt be correct to suppose that its value IS THERE already?

    Or when I load the second movie, although the value is in the main movie it must be reloaded to anywhere to be available to second movie (as when we pass a parameter to SWF in AS3)?

    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, the value is already there in the first movie. But the child movie cannot access it via the parent property until the right thing is the parent.

    The problem was that the code in the second movie was running before it had been removed from the loader and placed into the root. That means that its parent property was still giving you the Loader instead of the MovieClip with the variable. Using the ADDED_TO_STAGE event ensures that the code which references the parent does not get executed until the parent is the right thing.

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