Skip navigation
Currently Being Moderated

using random Array()

Jun 8, 2012 5:22 PM

Tags: #problem #error #3.0 #game #actionscript3 #actionscript_3.0

I am building a game and thought it would be cool to have an Array that holds different animated MC of short explosions as the player shoots down flyng objects.

 

The Array seems to work but I am getting error #1009 which is flooding my output window.

The game is made up of all external AS Classes. Each Explosion has it's own class with a simple:

public function enterNFrame(e:Event):void

                    {

 

 

                              if (this.currentFrame == totalFrames)

                              {

                                   if (this.parent.contains(this))

                                        {

                                                  SpaceGarbage.main.removeChild(this);

                                                  trace(this);

                                        }

                                          removeEventListener(Event.ENTER_FRAME, enterNFrame);

                              }

                    }

          }

This works great with one explosion but using the Array has,I suspect, created a loop where the other explosions are generated with nowhere to go.

This is the current Array:

public function kill()

                    {

                              var e1:Explosion = new Explosion(x, y);

                              var e2:Explosion2 = new Explosion2(x, y);

                              var e3:Explosion3 = new Explosion3(x, y);

                              var exploArray:Array = new Array(e1, e2, e3);

                              for(var j:int = 0; j < 1; j++)

                              {

                                        exploArray.push(j);

                              }

                       var myArray:Array = new Array();

           while (exploArray.length > 0)

           {

                    var r:int = Math.floor(Math.random() * exploArray.length);

                    myArray.push(exploArray[r]);

 

                    exploArray.splice(r, 1);

                    trace(exploArray);

 

 

           }

                       SpaceGarbage.main.addChild(myArray[0]);

 

ths a parial snapshot of my output window:

undefined

[object Timer]

[object MovieClip]

398 114

398 114

398 114

[object Explosion],[object Explosion2],0

[object Explosion2],0

[object Explosion2]

 

 

TypeError: Error #1009: Cannot access a property or method of a null object reference.

          at Explosion/enterNFrame()[/Users/Apple/Desktop/Kongregate_files copy 3-4newTest copyRR/source_files/Explosion.as:29]

TypeError: Error #1009: Cannot access a property or method of a null object reference.

          at Explosion2/enterNFrame()[/Users/Apple/Desktop/Kongregate_files copy 3-4newTest copyRR/source_files/Explosion2.as:28]

[object Explosion3]

 

 

I am new to Arrays so I'm sure that I am missing something imoprtant.

I just want to sort through the Array and select the first element and then use addChild();

 

Thanks for helping

 
Replies
  • Currently Being Moderated
    Jun 8, 2012 7:32 PM   in reply to jandy48

    Based on the error messages, it looks like lines 29 of Explosion.as and 28 of Explosion2.as are the sources of the errors.

     

    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).

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 9, 2012 5:27 AM   in reply to jandy48

    If you are dealing with arrays where you are removing elements that are stored in them, make sure that you keep the arrays in agreement with what 's left and are not targeting elements in them that were already removed.

     
    |
    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