PDA

View Full Version : TypeError driving me nuts!



womposan
August 13th, 2008, 07:53 PM
Hi,
Im creating a basic collision detection for my game. I have created two arrays: 1 for bullets and 1 for objects. For collision detection im using AS3.0's hitTestObject() function (it's enough for a space shooter :vader:). Upon collision (bullet vs. object) bullet is removed and object takes damage depending on players weapon. This is the collisionDetection function:



function collisionDetection(event:TimerEvent):void {
var i:int = myBulletArray.length;
var ii:int = myObjectArray.length;
while (ii--) {
while (i--) {
if (myBulletArray[i].hitTestObject(myObjectArray[ii])) {
removeChild(myBulletArray[i]);
myBulletArray[i] = null;
myBulletArray.splice(i,1);
myObjectArray[ii]._hp -= player.Damage(player._weapon);
}
}
}
}
CollisionDetection is run by a timed eventlistener (10 times per second):



var myTimer:Timer = new Timer(100);
myTimer.addEventListener(TimerEvent.TIMER, collisionDetection);
myTimer.start();
But when i run the game, following error occurs every timer tick:



TypeError: Error #1010: A term is undefined and has no properties.
at Game_fla::MainTimeline/collisionDetection()
at flash.utils::Timer/flash.utils:Timer::_timerDispatch()
at flash.utils::Timer/flash.utils:Timer::tick()
I have read that a possible cause for this is that i'm refering to mcs that haven't been created yet... Knowing this, i still haven't figured this out. If someone knows how to deal with this, any help would be appreciated!!:crying:

Iamthejuggler
August 14th, 2008, 05:46 AM
Are you sure the arrays are instantiated yet? When you create them make them empty arrays. Not sure if that is the problem but it's the first thing that sprung to mind.

womposan
August 17th, 2008, 05:59 PM
Are you sure the arrays are instantiated yet? When you create them make them empty arrays. Not sure if that is the problem but it's the first thing that sprung to mind.

How can i instantiate an array?:puzzled:

senocular
August 17th, 2008, 06:09 PM
How can i instantiate an array?:puzzled:

var variable = [];
//or
var variable = new Array();

womposan
August 17th, 2008, 06:30 PM
Nevermind, fixed by tracing values :). The error occurred in:



while (ii--) {
while (i--) {
if (myBulletArray[i].hitTestObject(myObjectArray[ii])) {
rList.push(myBulletArray[i]);
myBulletArray.splice(i,1);
myObjectArray[ii]._hp -= player.Damage(player._weapon);
rListHasItems = true;
}
}
}


...Because, when the loop had checked collisions with first object vs all bullets, the bulletloop remained 0, so getting the bullet var back to "start" before every loop (vs object) solved the case! As below:



while (ii--) {
i = myBulletArray.length;
while (i--) {
if (myBulletArray[i].hitTestObject(myObjectArray[ii])) {
rList.push(myBulletArray[i]);
myBulletArray.splice(i,1);
myObjectArray[ii]._hp -= player.Damage(player._weapon);
rListHasItems = true;
}
}
}

womposan
August 17th, 2008, 06:33 PM
var variable = [];
//or
var variable = new Array();

Oh yeah, that looks familiar :). I had done that, just wasn't sure with the terminology :D

And thanks for the fast reply!