A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: accessing a variable in a parent SWF

  1. #1
    Member
    Join Date
    Jul 2009
    Location
    Florianópolis
    Posts
    81

    accessing a variable in a parent SWF

    Hello!

    I have an SWF file that loads another SWF file dynamically.

    I've managed to make the parent SWF receive a variable from the php page that loads it, via javascript, it works well:

    Code:
    try {
        var keyStr:String;
        var valueStr:String;
        var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
        for (keyStr in paramObj) {
            valueStr = String(paramObj[keyStr]);
                 }
    } catch (error:Error) {
     }
    But then I want the nested SWF to access this variable and then do some function.
    in this case, the nested swf has a line

    Code:
    cmd:int = -1;
    the nested swf should be able to receive a number, and pass to a function that would load its respective image from an array. otherwise (if it doesn't receive a number ) it won't do nothing (load no image).

    that worked well too, as I tested by manually substituing the number -1 in the statement above with a positive number, inside the flash interface.

    Now what should be the simplest part of it (at least in my imagination) is being a pain in the ***.

    I've tried using the try/catch statement in the nested SWF like this:

    Code:
    var paramObj:Object = LoaderInfo(MovieClip(parent).loaderInfo).parameters;
    or

    Code:
    var paramObj:Object = LoaderInfo(parent.loaderInfo).parameters;
    or trying to access valueStr in the parent SWF file:

    Code:
    MovieClip(parent).valueStr;
    and parent.valueStr;
    but no success.

    Please help!
    Thanks in advance!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Why wouldn't you include the error messages you're getting?

    I'll guess that you are loading the swf with a Loader and it's still a child of that loader. You could add it to the parent swf directly in a complete handler for that loader, or you could use parent.parent. But what you should really do is have a function in the loaded swf that takes the value you want as a parameter. Then call that function from the parent swf when you want to. Or you could have a function that saves a local variable with the value and call that in the complete handler.

    Let's say you want to keep a value called "foo". You've retrieved this value in the parent somehow, it doesn't matter for this exercise.

    loaded swf has code more or less like this:
    Code:
    var foo:String; //loaded swf's foo variable
    
    function setFoo(f:String):void{
      foo = f;
    }
    and the loading swf has code something like this:
    Code:
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete);
    loader.load(new URLRequest("myChild.swf"));
    
    function handleComplete(e:Event):void{
      var l:Loader = LoaderInfo(e.currentTarget);
      var clip:MovieClip = MovieClip(l.content);
      clip.setFoo(foo);
    }

Tags for this Thread

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