ROUNDPAWS
ROUNDPAWS

Reputation: 1

Actionscript 3 passing variable from parent to child

I am still having problems so I made a very basic main.swf that loads sub.swf.

Sub.swf has 2 rectangle movieclips (red and blue) that have their alpha = 0.

Both swfs compile fine and no errors but the red alpha does not get turned on so the value of "spanish" from main.swf variable must not be getting recognised in sub.swf. Any ideas?

Main.swf actionscript:

import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;

var child_loader:Loader = new Loader();

var req:URLRequest = new URLRequest("sub.swf");

child_loader.load(req);

child_loader.contentLoaderInfo.addEventListener(Ev ent.COMPLETE, done);

function done(e:Event):void{
    addChild(child_loader);
    MovieClip(child_loader.content).language = "spanish";
}

sub.swf actionscript:

import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;

function callFunction(_txt:String):void{
    var sublanguage = (MovieClip(parent.parent).language)

    if (sublanguage == "spanish"){
    red.alpha = 1;
    }else if (sublanguage == "english"){
        blue.alpha = 1;
    }
}

Upvotes: 0

Views: 1338

Answers (2)

Amy Blankenship
Amy Blankenship

Reputation: 6961

The problem is that AS3 is not dynamic in the way AS2 was, so the child document needs to have a property or method that the parent knows is there.

One way to do this is relatively simple:

package {
   public class ChildClass extends MovieClip {
       protected var _property:String;

       public function get property():String {
          return _property;
       }
       public function set property(value:String):void {
          if (value != _property) {
              _property = value;
              //do something, because now the property has been set
          }
       }
   }
}

You'd just apply that as the Document Class to the swf you're loading, and then in your onLoaded function, you'd do something like:

//cast to ChildClass, so you will know you have the property available
var childClass:ChildClass = Loader(event.currentTarget).contentLoaderInfo.content as ChildClass;

//if the variable content is null, the swf had a different Document Class
if (childClass) {
    //now you can set your variable, you're good
    childClass.property = 'foo';
}

Like most things in Actionscript, you can do it the easy way (shown above), or you can do it the right way. The right way is to use an Interface. What an Interface is is the "blueprint" for a Class. In this case, it would need to say that any Class that implements that Interface would always have a property getter and a property setter. You can't just use a variable, because Interfaces only allow functions.

But at any rate, the advantage of using the Interface, especially in your case, is that you will not have to compile in the specific Class associated with the swf that you're loading. Instead, you'd just need to compile in the "contract" for the Class, which is much lighter weight. But because the player won't cast the contentLoaderInfo.content to ISomeInterface unless it fulfills that contract, you can confidently set that property once you have done the casting.

Upvotes: 1

lostPixels
lostPixels

Reputation: 1343

You should be able to set up a function in your child movie that can be called from your parent. You could pass anything you want as an argument.

Upvotes: 0

Related Questions