如何从加载的 swf 访问父 movieclip 的变量
我有一个加载外部 swf 的 swf。加载器 swf 有一个变量,我想从加载的 swf 访问该变量。我使用 MovieClip(parent.parent).myVar
但它返回此错误
TypeError:错误#1009:无法访问空对象引用的属性或方法。 在card01_fla::MainTimeline/frame1()
加载器 swf 有一个自定义文档类,我认为这是问题所在。
提前致谢。
编辑
这是主swf的as
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.text.TextField;
/**
* ...
* @author chchrist
*/
public class CardSender extends Sprite
{
private var loader:Loader;
private var viewButtonArray:Array;
private var sendButtonArray:Array;
private var cardContainer:Sprite;
public var testVar:String
public function CardSender() {
testVar = "it worked!!!";
init();
}
private function init() {
viewButtonArray = [cardsenderMC.view01, cardsenderMC.view02, cardsenderMC.view03, cardsenderMC.view04];
for each( var viewButton in viewButtonArray) {
viewButton.buttonMode = true;
viewButton.addEventListener(MouseEvent.CLICK, onViewClick);
}
}
private function onViewClick(e:MouseEvent):void {
var cardName = "card"+e.target.name.substring(4)+".swf";
loader = new Loader();
loader.load( new URLRequest(cardName));
loader.contentLoaderInfo.addEventListener(Event.INIT, onCardInit);
}
private function onCardInit(e:Event):void {
addChild(loader);
var content:* = loader.content;
content.closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
}
private function onCloseClick(e:MouseEvent):void {
removeChild(loader);
}
}
}
,我尝试使用此代码从加载的swf访问测试变量
trace(MovieClip(parent.parent).testVar);
EDIT#2
如果我addChild(loader )
不在 onCardInit 中,而是在 onViewClick 中,并且在加载的 swf 的第一帧中,我有 Object(parent.parent).testVar
它可以工作......但为什么它不起作用想要进入 onCardInit 函数内部吗?
I have a swf which load an external swf. The loader swf has a variable which i want to access from the loaded swf. I use MovieClip(parent.parent).myVar
but it return this error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at card01_fla::MainTimeline/frame1()
The loader swf has a custom Document Class which i think is the problem.
Thanks in advance.
EDIT
This is the as of the main swf
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.text.TextField;
/**
* ...
* @author chchrist
*/
public class CardSender extends Sprite
{
private var loader:Loader;
private var viewButtonArray:Array;
private var sendButtonArray:Array;
private var cardContainer:Sprite;
public var testVar:String
public function CardSender() {
testVar = "it worked!!!";
init();
}
private function init() {
viewButtonArray = [cardsenderMC.view01, cardsenderMC.view02, cardsenderMC.view03, cardsenderMC.view04];
for each( var viewButton in viewButtonArray) {
viewButton.buttonMode = true;
viewButton.addEventListener(MouseEvent.CLICK, onViewClick);
}
}
private function onViewClick(e:MouseEvent):void {
var cardName = "card"+e.target.name.substring(4)+".swf";
loader = new Loader();
loader.load( new URLRequest(cardName));
loader.contentLoaderInfo.addEventListener(Event.INIT, onCardInit);
}
private function onCardInit(e:Event):void {
addChild(loader);
var content:* = loader.content;
content.closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
}
private function onCloseClick(e:MouseEvent):void {
removeChild(loader);
}
}
}
and I am trying to access the test variable from the loaded swf with this code
trace(MovieClip(parent.parent).testVar);
EDIT#2
If I addChild(loader)
not in the onCardInit but in the onViewClick and in the first frame of the loaded swf i have Object(parent.parent).testVar
it works...but why it doesn't want to be inside the onCardInit function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您只有一个
loader
变量,并且要创建四个Loader
实例并将它们分配给它 - 因此,如果您快速连续单击所有四个Loader
实例,>loader
变量将有效包含与上次单击相对应的Loader
对象。您的所有addChild
调用都将作用于同一个Loader
对象 - 其他对象将不会被添加,并且它们的父对象将继续为null
进行以下操作代码更改:
回答问题中的以下更新:
同样,这可能是因为您正在添加不同的
loader
对象 - 您是否单击了多个视图按钮?单击查看按钮并等待其加载以查看其工作情况。You have just one
loader
variable and you're creating fourLoader
instances and assigning them to it - so if you are clicking all four of them in quick succession, theloader
variable will effectively contain theLoader
object corresponding to the last click. All youraddChild
calls will act on that sameLoader
object - the others will not be added and their parents will continue to benull
Make the following changes to the code:
Answer to the following update in the question:
Again, that might be because you are adding a different
loader
object - are you clicking on more than one viewbuttons? Click on a view-button and wait till it is loaded to see it working.父.父是错误的。
尝试使用:
您必须将第一个父级强制转换为某些类(例如 MovieClip),然后获取这个新 MovieClip 的父级,然后获取内部变量。
parent.parent is wrong.
try using:
You have to cast the first parent to some class like MovieClip and then get this new MovieClip's parent and then getting inside variable.
尝试追踪parent和parent.parent,我确信这是因为有一个额外的“parent”
try tracing parent and parent.parent, I'm sure it's because of an aditional "parent"
loader.content 不是影片剪辑,因此它不具有相同的功能。尝试
myLoadedClip:MovieClip = MovieClip(loader.content);
然后你的 myLoadedClip.parent.myVar 应该显示
你可能希望你的处理程序在 Event.COMPLETE 而不是在 Event.INIT 上触发
loader.content is NOT a movieclip, so it doesn't have the same capabilities. try
myLoadedClip:MovieClip = MovieClip(loader.content);
then your myLoadedClip.parent.myVar should show up
you might want your handler to fire on a Event.COMPLETE instead of on an Event.INIT
加载的 swf 中的这行代码就解决了这个问题! ;)
MovieClip(MovieClip(parent.parent).root).play();
This only line code in loaded swf resolves it! ;)
MovieClip(MovieClip(parent.parent).root).play();