Skip navigation
Currently Being Moderated

Error: Cannot access a property or method of a null object reference. at enemy/loop()

Aug 9, 2012 6:16 AM

Tags: #problem #flash #action #actionscript #ac3 #game #actionscript3

Hi everybody, i'm learning flash for a few days. I want to write a simple game.. We move a "gun", and if we click mouse, the gun send a bullet. Then if bullet hit the enemy, enemy and bullet disapear. Unfortunately, i have a error That's my fragment of code:


// main (in game.fla):

var bulletholder:MovieClip = new MovieClip();



function shoot(e:Event):void


          player.y = 400;


          var bul:MovieClip = new bullet();

          bul.x = player.x+21;

          bul.y = player.y-10;

          bul.width = 30;

          bul.height = 30;





// this is code in

public class enemy extends MovieClip {


                    private var core:Object;



                    public function enemy() {

                              addEventListener(Event.ENTER_FRAME, loop);

                              x = Math.random()*550;

                              y = -100;

                              core = MovieClip(root);



                    private function loop(e:Event) {

                              y += 4;


                              var ilosc:int = core.bulletholder.numChildren; // in this moment, debug show error !


                              for(var i:int = 0; i < ilosc; i++)


                                        var target:MovieClip = core.bulletholder.getChildAt(i);




                                                  core.bulletholder.getChildAt(i).remove(); // remove() is my function from



                                                  removeEventListener(Event.ENTER_FRAME, loop);





                              if(y > 600)


                                        removeEventListener(Event.ENTER_FRAME, loop);








Please help me, I'm stuck here now for 2 days


And sorry for any mistakes, i don't know english very well.

  • Currently Being Moderated
    Aug 9, 2012 6:56 AM   in reply to SeaMonster1311

    You should include the complete error message in your posting.


    The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....


    - is declared but not instantiated

    - doesn't have an instance name (or the instance name is mispelled)

    - does not exist in the frame where that code is trying to talk to it

    - is animated into place but is not assigned instance names in every keyframe for it

    - is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).


    If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.

    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 11:46 AM   in reply to SeaMonster1311

    When you remove a child you reduce the number of children and all children above that index shift down to fill that gap.  Your loop counter "ilosc" is based on the number of children before you removed any.  So as soon as you get to an " i " value where there is no longer a child, you will encounter that error.


    One way to overcome it is to work the loop in reverse, going from the highest child index to the lowest, as in....


            for(var i:int = ilosc-1, i >-1; i--)

    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 12:55 PM   in reply to SeaMonster1311

    You're welcome

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points