Hi, I’ve lurked these parts for a while gaining some invaluable information along the way, However I have a problem that doesn’t seem to have come up yet. Help would be greatly appreciated :puzzled:
I am currently building a game and want the obstacles to be generated dynamically and then fall towards the player.
In creating my obstacles I am sending them to an array, which is then looped through to get any existing obstacles to move down the screen. There are four different types of obstacles and each is stored in a different array, then those arrays are stored in a master array.
Here is the code that defines the arrays
var blackArray:Array = new Array();// stores black walls
var whiteArray:Array = new Array();// stores white walls
var sideArray:Array = new Array();//stores side walls
var midArray:Array = new Array();//stores dividing walls
var wallArray:Array = [blackArray, whiteArray, sideArray, midArray]; //stores obstacles
The code that creates a wall obstacle and then adds it to the array
function createBlackwall(xco:int, yco:int, wid:int, hei:int):void {
var myBlackwall = new blackwall(); //creates an instance of the movieclip from library
stage.addChild(myBlackwall); //places the instance on the stage
myBlackwall.x = xco; //this line and the three below set properties for the movieclip
myBlackwall.y = yco;
myBlackwall.width = wid;
myBlackwall.height = hei;
blackArray.push(myBlackwall); //Sends the movieclip into the array
The functions that deal with the other obstacles are all but identical to this one
And finally the bit that seems to be causing the problem my comments are for the intended result, though not necessarily what the code actually does
function levelMove() {
for each (var item:Array in wallArray) { //looks through the arrays in 'wallArray'
for each (var i:Object in item) { //then looks through the items in the array
if (item*.y == undefined) { //if nothing exists in this location
break; // leave this loop
} else {
item*.y += 10; //otherwise move the movie clip down the stage by 10 pixels
if (item*.y > 480) { //if the movie clip leaves the stage
delete item*; //delete it from the array
}
}
for each(var j:Object in item) { //this is the same loop as before
if (item[j] == undefined) { //should an item have been deleted from the array
item[j].shift(); //remove the element
} else {
break; //once all deleted elements have been shifted leave this loop
}
}
}
}
}
What happens in practise is that the first instance of an obstacle slides off the screen, then Actionscript returns error 1010. I can’t see what I’m doing wrong, though I am teaching myself the concepts as I need to use them.
Again any help would be greatly appreciated :puzzled:
P.S. though this is for a game, i thought it better here as it’s more a question about a general programming concept