1

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.

9
  • Did you googled "Actionscript 3 TypeError: Error #1009" ? I also noticed that you did not declare your buton object var buton:MovieClip; Jun 19, 2013 at 13:52
  • 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
    Jun 19, 2013 at 14:21
  • Did you trace your DisplayList to see what's really available ? Jun 19, 2013 at 14:29
  • hm. that is interesting I traced it now and here is the results: buton: null / getChildAt(i): [object Shape]
    – roser137
    Jun 19, 2013 at 14:34
  • You got it I think, Shape is not a MovieClip. as operator will return null: as operator documentation Jun 19, 2013 at 14:38

1 Answer 1

0

*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);
       }
   }
}
3
  • I don't see a button_start, you mean button from this line: getChildAt(i).name.search("button")? Jun 20, 2013 at 15:03
  • Shape is not an InteractiveObject so Shape will not be appropriate for a button on screen. See the parent class DisplayObject to see the fork. Jun 20, 2013 at 15:27
  • button_start is a simple, one frame MovieClip symbol added into Splash symbol with Flash IDE. I don't have enough knowledge about InteractiveObject object or DisplayObject but I think for a simple one frame Shape would be enough.
    – roser137
    Jun 21, 2013 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.