Well, I solved it 2 days ago more o less, but was quite busy fixing another problem so I was not sure to post it if in the end it was not usable.
The problem was that the eventListeners in the Walls class were a problem when adding childs, so I left the class in blank, and moved them in the document class. I also added the removeChild
in the spawn_wall_piece
function too. I also created a function to initialize and adequate the array, so that when wallArray.length == 5
, the function was called, the walls before the ones in stage eliminated, and other walls created. As for the collision, as I was facing problems, I put the addItem
of the CDK method inside the spawn_wall_piece
instead of the function to adequate the array. Well, this is the functional code:
//In the constructor
//Calling to the generating/adequating wallArray function
adequate_array_of_walls(true);
myWallCollisionList.addItem(wallArray[0]);
myWallCollisionList.addItem(wallArray[1]);
//...
//The function called by enter_frame
function update_game(e:Event){
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
adequate_array_of_walls(false);
}
//...
//The functions called of the wall generation:
function spawn_wall_piece(){
index++;
wallArray[index].x = (wallArray[index - 1].x + wallArray[index - 1].width);
wallArray[index].y = 0;
stage.addChild(wallArray[index]);
myWallCollisionList.addItem(wallArray[index]);
myWallCollisionList.removeItem(wallArray[index - 2]);
stage.removeChild(wallArray[index - 2]);
}
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);
}
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 and rearranging it so it doesn't grow bigger and bigger
}else{
for(var a:Number = 0; a < index - 1; 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);
}
}
//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;
}