Search code examples
actionscript-3flashexternalrootstage

AS3 externally loaded swf to control stage and MovieClips and their children?


I have 2 swf files. One is loaded and loading an externally loaded swf into it.

Here is the code in the first loaded swf: #1

var logo:Loader = new Loader();
logo.load(new URLRequest("images/Logo.png"));
logo.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadLogo);
function LoadLogo(e:Event):void
{
    addChild(logo);

}

/// The SWF I AM LOADING 

var Beau:Loader = new Loader();
Beau.load(new URLRequest("Beau.swf"));
Beau.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadBeau);

function LoadBeau(e:Event)
{
    addChild(Beau);

}

NOW HERE IS THE CODE FOR THE BEAU SWF LOADED: #2

import flash.display.MovieClip;


bird.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);

function fl_MouseClickHandler(event:MouseEvent):void
{
   ////   I WANT TO BE ABLE TO ADD CODE TO CONTROL MOVIECLIPS 
   ////   AND CHILDREN form this externally loaded swf. How do
   ////   I do that? Below code does not work.

var logo:MovieClip;
var ControlLogo:MovieClip = MovieClip(logo.content);
    ControlLogo.alpha = .3;

}


EDIT

ok your code and tutorials are great. I am having major SANDBOX issues now when I make the clip load from an external URL. I am also coding for an ANDROID using FLASH CS5.5. Its not allowing me to use Security.allowDomain("*");

BOTTOME LINE IS THE NEWLY LOADED SWF can not access the PARENT WITHOUT PERMISSION BUT I DON'T KNOW HOW TO GIVE IT PERMISSION.... using the AIR/ANDROID PLAYER.


Solution

  • use property parent

    parent gives loader

    parent.parent gives swf#1

    So you can write function as given below

    function fl_MouseClickHandler(event:MouseEvent):void
    {
    
        var swf1:Object = parent.parent;
    
        var logo:Loader = swf1.logo;
        var ControlLogo:Bitmap = Bitmap(logo.content); // because logo loads an image
        ControlLogo.alpha = .3;
    
    
    }
    

    make sure that logo is not a local variable, but global and public in swf#1

    public var logo:Loader = new Loader();
    

    EDIT:

    logo:Loader gets added to SWF#1. Loads image, so content is Bitmap.

    beau:Loader gets added to stage. Loads swf (SWF#2), so content is MovieClip.

    so now

    root is SWF#1

    parent of beau is SWF#1

    parent of SWF#2 is beau and parent of beau is SWF#1

    so for SWF#2, SWF#1 is parent of parent so parent.parent

    If you are not creating public variables, you can manage this by using property name.

    var logo:Loader = new Loader();
    logo.name = "logo";
    ...
    ...
    
    var beau:Loader = new Loader();
    beau.name = "beau";
    ...
    ...
    

    Then anywhere in swf#2

    var swf1:Object = parent.parent;
    
    var logo:Loader = Loader(swf1.getChildByName("logo"));
    ....
    ....
    

    For accessing the content it is recommended to use type casting as I have shown

    var ControlLogo:Bitmap = Bitmap(logo.content); // because logo loads an image
    

    so that you can check as shown below and avoid runtime errors

    var ControlLogo:Bitmap = Bitmap(logo.content); 
    if(ControlLogo){
    
    }
    

    If you want to so something irrespective of content do as shown below

    var ControlLogo:Object = logo.content; 
    if(ControlLogo && ControlLogo.hasOwnProperty("alpha")){
       ControlLogo.alpha = 0.4;
    }