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
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).
I changed each Explosion Class to include:
if (this.currentFrame == totalFrames)
{
if (SpaceGarbage.main.contains(this))
{
SpaceGarbage.main.removeChild(this);
trace(this);
}
removeEventListener(Event.ENTER_FRAME, enterNFrame);
}
instead of:
SpaceGarbage.main.removeChild(this);
I also seperated the kill() function into two seperate functions in another Class(EnemyShip) that contained the hitTestObject.
These two changes have made a difference.
Occasionally, however I am still getting this error in the output:
"
[object Explosion3]
[object Explosion2]
[object Explosion]
TypeError: Error #1034: Type Coercion failed: cannot convert 1 to flash.display.DisplayObject.
at EnemyShip/kill()[/Users/Apple/Desktop/SpaceGarbage_files copy 3-4newTest copyRR/source_files/EnemyShip.as:147]
at Bullet/enterNewFrame()[/Users/Apple/Desktop/SpaceGarbage_files copy 3-4newTest copyRR/source_files/Bullet.as:66]
"
I am trying to understand #1034
If I go to EnemyShip_line#147 /this is where myArray() is added to the stage. maybe the Array is not keeping up with call for an explosion???
The error doesn't seem to slow down performance but I am interested in getting rid of it and learning more about how AS3 works.
I will read more about Array's and see if I can make sure I understand what is happenning between the
myArray.push(exploArray[r];
AND
explorArray.splic(r, 1);
Thanks
Hi,
After lots of testing, I'm satisfied I found the original problem and thought I would post this for anyone just starting out using Arrays like I am.
At the beginning, in my original post, my forLoop was:
for(var j:int = 0; j < 1; j++)
{
exploArray.push(j);
}
now I have changed it to:
| for(var j:int = 0; j < 0; j++) | ||||
| { | ||||
| exploArray.push(j); | ||||
| trace(j, "first exploArray") | ||||
| } |
The j is now less then 0, which is what it should be as there are three elements in the array not two.
North America
Europe, Middle East and Africa
Asia Pacific