简体   繁体   中英

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. I have a symbol and an AS3 class linked to this symbol called Splash (which represents intro screen of Game) and a StartButton symbol. StartButton symbol placed in Splash symbol using Flash IDE given instance name of 'button_start'.

In Splash.as I'm trying to access StartButton's ADDED_TO_STAGE event handler but i got null pointer exception. 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 ? Where is wrong couldn't figure it out.

*button_start* is not an instance of MovieClip, it is a 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);
       }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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