[英]Can't access child MovieClips in ADDED_TO_STAGE event handler
problem solved. 问题解决了。 please see comments. 请查看评论。 I can't answer my own question due to reputation points. 由于声望高低,我无法回答自己的问题。
I'm trying to develop a simple flash game. 我正在尝试开发一个简单的Flash游戏。 I have a symbol and an AS3 class linked to this symbol called Splash (which represents intro screen of Game) and a StartButton symbol. 我有一个符号以及一个链接到该符号的AS3类,称为Splash(代表游戏的简介屏幕)和一个StartButton符号。 StartButton symbol placed in Splash symbol using Flash IDE given instance name of 'button_start'. 给定实例名称“ button_start”,使用Flash IDE将StartButton符号放置在Splash符号中。
In Splash.as I'm trying to access StartButton's ADDED_TO_STAGE event handler but i got null pointer exception. 在Splash.as中,我试图访问StartButton的ADDED_TO_STAGE事件处理程序,但出现空指针异常。 Here is code: 这是代码:
public function Splash() {
trace("splash const");
addEventListener(Event.ADDED_TO_STAGE, ats);
super();
}
public function ats(e:Event) {
trace("splash ats");
var i:int = 0;
for (i=0;i<numChildren;i++) {
if (getChildAt(i).name.search("button")) {
trace("bulundu");
buton = getChildAt(i) as MovieClip;
buton.addEventListener(Event.ADDED_TO_STAGE, butonats);
}
}
}
function butonats(e:Event) {
trace("buton ats");
}
Result: 结果:
splash const
splash ats
bulundu
TypeError: Error #1009: Boş nesne başvuru özelliğine veya yöntemine erişilemiyor.
at Splash/ats()
Doesn't ADDED_TO_STAGE run's when all children ready ? 当所有孩子都准备就绪时,ADDED_TO_STAGE不会运行吗? Where is wrong couldn't figure it out. 哪里出了问题无法解决。
*button_start* is not an instance of MovieClip, it is a Shape. * button_start *不是MovieClip的实例,它是一个Shape。 So making this changes to code solved the problem. 因此,对代码进行此更改可以解决问题。
var buton:Shape;
public function Splash() {
trace("splash const");
addEventListener(Event.ADDED_TO_STAGE, ats);
super();
}
public function ats(e:Event) {
trace("splash ats");
var i:int = 0;
for (i=0;i<numChildren;i++) {
if (getChildAt(i).name.search("button")) {
trace("bulundu");
buton = getChildAt(i) as MovieClip;
buton.addEventListener(Event.ADDED_TO_STAGE, butonats);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.