I'm having problems when using the CDK (Collision Detection Kit) with elements in an array. It keeps getting me false positives of collision.
I'm making a game in which the player controls a “ship”, that has to avoid both enemies and the terrain in which it moves through, like a “tunnel” with obstacles. To make the “tunnel” random every time, I emit “pieces” of different walls into stage so it seems like an infinite tunnel with obstacles inside. In order to do this, I created an array, called wallArray, which stores the display objects from the class Walls, which is the class where in each frame, there's a different shape of wall.
Everything works like a charm, except the collision, which uses CDK. I'm using the regular CollisionList method:
//On the document class, on the enter frame (eventListener) function:
wallArray[(index - 1)].x -= wall_mov_speed;
wallArray[index].x -= wall_mov_speed;
if(wallArray[index].x < 0){
spawn_wall_piece();
}
if(index == 5){
//We call this function for cleaning the array and readding walls to it
adequate_array_of_walls(false);
}
if(wall_mov_speed < 15){
wall_mov_speed += 0.009;
}
wall_collisions = myWallCollisionList.checkCollisions();
if(wall_collisions.length > 0){
trace("hit!");
if(myShip.visible == true){
//We only kill the ship if it's visible, if not, it means it is already dead
Ship.receiveDamage(Ship.max_health);
}
wall_collisions = 0 as Array;
}
And here we have the functions for rearranging the wallArray, also in the document class (and where I add the elements to the CollisionList):
function adequate_array_of_walls(init:Boolean):void{
//This only executes if we are initialitizing the array
if(init == true){
for(index = 0; index < 10; index++){
var aWall:Walls = new Walls();
randomize = Math.floor(Math.random()*4) + 1;
aWall.gotoAndStop(randomize);
wallArray.push(aWall);
myWallCollisionList.addItem(wallArray[index]);
}
wallArray[0].gotoAndStop(1);
stage.addChild(wallArray[0]);
wallArray[1].x = 800;
wallArray[1].y = 0;
stage.addChild(wallArray[1]);
//if not, then we are just cleaning it so it doesn't grow bigger and bigger
}else{
for(var a:Number = 0; a < index - 1; a++){
myWallCollisionList.removeItem(wallArray[a]);
wallArray.splice(0,1);
}
for(a = index - 1; a < (10-2); a++){
var aWall2:Walls = new Walls();
randomize = Math.floor(Math.random()*4) + 1;
aWall2.gotoAndStop(randomize);
wallArray.push(aWall2);
myWallCollisionList.addItem(wallArray[a]);
}
}
//Then, either way, we tell index to be 1 since the reference in the function is [index - 1] and [index], so it starts with [0] and [1]
index = 1;
}
function spawn_wall_piece(){
index++;
wallArray[index].x = (wallArray[index - 1].x + wallArray[index - 1].width);
wallArray[index].y = 0;
stage.addChild(wallArray[index]);
stage.removeChild(wallArray[index - 2]);
}
Well, as I said, everything works except the collision, it keeps me detecting collisions even though there's none. I was troubled that it was the walls' too intrincate shape, but tried this method before even with mazes and worked perfectly. I don't know what could cause this, am I missing something?
I would appreciate very much any help or comment.
Thank you in advance.
EDIT: I post the full code related to the wall collision to cover every possibility just in case:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import com.coreyoneil.collision.CollisionList;
//...
public class Game extends MovieClip {
//...
//Wall generation variables
static var wall_mov_speed:Number;
var randomize:Number;
var wallArray:Array = new Array();
var index:int = 0;
//
//Wall collision variables
var myWallCollisionList:CollisionList = new CollisionList(myShip);
var wall_collisions:Array = new Array();
//
//...
public function Game() {
//...
//Movement speed of the walls
wall_mov_speed = 8;
//Calling to the generating/adequating wallArray function
adequate_array_of_walls(true);
//
//Collision managements
wall_collisions = 0 as Array;
//
//...
}
myWallCollisionList.addItem
. How many items are there? You are not supposed to clear it up? – zehelvion Feb 4 '14 at 11:51a
and where and when do you remove the walls? – zehelvion Feb 4 '14 at 12:16