1
votes

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.

1
Did you googled "Actionscript 3 TypeError: Error #1009" ? I also noticed that you did not declare your buton object var buton:MovieClip;Eric Lavoie
thanks for comment, I declared buton in class level. I also google TypeError 1009. It usually happens when you referance object when it is not ready. They suggest to use ADDED_TO_STAGE event as I do. Couldn't find anything different.roser137
Did you trace your DisplayList to see what's really available ?Eric Lavoie
hm. that is interesting I traced it now and here is the results: buton: null / getChildAt(i): [object Shape]roser137
You got it I think, Shape is not a MovieClip. as operator will return null: as operator documentationEric Lavoie

1 Answers

0
votes

*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);
       }
   }
}