PDA

View Full Version : [AS3] Looping through array for collision


rendor
09-24-2009, 04:01 PM
Hi,

I started making a simple game, but the collision code has me puzzled.
It throws me a typeerror 1034.
1034 Type Coercion failed: cannot convert Object@d3c59e9 to a flash.display.DisplayObject.

these bits is the code I think break it.

Basically I put every 'Wall' object in an array, after that I loop trough the array and do a hitTestObject.
Somehow the objects in the array are no longer considered to have inherited from display object.

the bits:

function examineLevel() {
//trace("level col array");
collision = new Array();
for (var i:int=0; i<this.gamelevel.numChildren; i++) {
var mc=this.gamelevel.getChildAt(i);
if (mc is Wall) {
var collObject:Object = new Object();
collObject.mc=mc;
collision.push(collObject);
trace(collision.length);
//return;

and later on:

function testColls() {
//trace("testColls");
for (var i in collision) {
if (player.hitTestObject(collision[i])) {
trace("collision");

I tried changing Object into MovieClip, but then there are no errors, and the collision doesn't work.

If anyone has an idea, I would be very gratefull.

complete code is here btw, in case anyone wants to have a look:
http://cid-5a94a1f6ac0c568d.skydrive.live.com/self.aspx/Public/Coll.rar

rrh
09-24-2009, 06:47 PM
Try:
player.hitTestObject(MovieClip(collision[i].mc))

krayzeebean
09-24-2009, 06:53 PM
Just put the mc directly into the array, no need to try to make it a property of a new object.


function examineLevel() {
//trace("level col array");
collision = new Array();
for (var i:int=0; i<this.gamelevel.numChildren; i++) {
var mc=this.gamelevel.getChildAt(i);
if (mc is Wall)
collision.push(mc);
}
}

rendor
09-25-2009, 02:48 PM
HURRAY!

Both ways actually work! thanks for helping me out.