简体   繁体   English

AS3从外部加载SWF来控制舞台,MovieClips及其子级?

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

I have 2 swf files. 我有2个SWF文件。 One is loaded and loading an externally loaded swf into it. 一个已加载,并将一个外部加载的swf加载到其中。

Here is the code in the first loaded swf: #1 这是第一个加载的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 现在,这里是加载的BEAU SWF的代码: #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. 当我从外部URL加载剪辑时,我现在遇到主要的SANDBOX问题。 I am also coding for an ANDROID using FLASH CS5.5. 我也正在使用FLASH CS5.5编码ANDROID。 Its not allowing me to use Security.allowDomain("*"); 它不允许我使用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. 底线是新加载的SWF在没有权限的情况下无法访问父级,但我不知道如何授予其权限...。使用AIR / ANDROID PLAYER。

use property parent 使用财产parent

parent gives loader parent给装载者

parent.parent gives swf#1 parent.parent给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 确保logo不是局部变量,而是swf#1中的全局变量和公共变量

public var logo:Loader = new Loader();

EDIT: 编辑:

logo:Loader gets added to SWF#1. logo:Loader添加到SWF#1。 Loads image, so content is Bitmap. 加载图像,所以内容就是位图。

beau:Loader gets added to stage. beau:Loader添加到舞台。 Loads swf (SWF#2), so content is MovieClip. 加载swf(SWF#2),因此内容为MovieClip。

so now 所以现在

root is SWF#1 根是SWF#1

parent of beau is SWF#1 beau父母是SWF#1

parent of SWF#2 is beau and parent of beau is SWF#1 SWF#2的父级是beau ,而beau父级是SWF#1

so for SWF#2, SWF#1 is parent of parent so parent.parent 因此对于SWF#2,SWF#1是父级的父级,因此parent.parent

If you are not creating public variables, you can manage this by using property name . 如果不创建公共变量,则可以使用属性name进行管理。

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

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

Then anywhere in swf#2 然后在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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM