In my programming class, I need to create a Tiles game with AS3 (like zelda). On a map, the tiles are initialised black in an array, and, after that, they are changing randomly each time the leftpick of a song reach a certain value.
When the tiles change, I need to update the Array with the new value. Actually, I always get an Error #1010: A term is undefined and has no properties.
This is how the tiles are initialised:
public var tMap: Array = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]; //This is the map. Every tiles are set to 1 (black)
//this is the array to update
// Good to know : each tiles are a single frame of a MovieClip
private var grid: MovieClip; //stock tiles like an array
private var nbRow: int = 6; //nb of row on the map
private var nbCol: int = 12; //nb of col on the map
private var oneTiles: Tiles; // One object Tiles
private var t: Tiles; //object Tiles
private function createGrid(): void {
grid = new MovieClip();
addChild(grid);
for (var r: int = 0; r < nbRow; r++) {
for (var c: int = 0; c < nbCol; c++) {
t = new Tiles();
t.x = t.width * c;
t.y = t.height * r;
grid.addChild(t);
}
}
}
//place the tiles on the good frame with tMap
private function displayTiles(): void {
var i: int = 0;
for (var r: int = 0; r < nbRow; r++) {
for (var c: int = 0; c < nbCol; c++) {
var t: Tiles;
t = Tiles(grid.getChildAt(i));
t.gotoAndStop(tMap[r][c]);
i++;
}
}
}
This is the function called everytime the leftpeak reach the value
private function resetTiles(): void {
//Change tiles randomly
for (var i: int = 0; i < grid.numChildren; i++) {
// loop through all child element of a movieclip
oneTiles = grid.getChildAt(i) as Tiles;
// Cast display objects to Tiles class.
oneTiles.getTiles();
// Call getTiles method
//30% chance gotoAndStop(1) black tiles
//70% chance gotoAndStop(2) white tiles
This is the source of the problem : to update the array, I added this to resetTiles(). Whit it, I always get an Error #1010: A term is undefined and has no properties. :
private var posX: uint = 0; //global
private var posY: uint = 0; //global
tMap[posX][posY] = oneTiles.getFrame();
if(posX == 11 && posY != 5){
posX = 0;
posY++;
}else if(posX == 11 && posY == 5){
posX = 0;
posY = 0;
}else{
posX++;
}
trace(posX);
trace(posY);
}
So, where's the problem ? Normaly, with this code, each time a tile is changed, the good tile in tMap shall be updated.
I did some test, and what seems to be the source of the problem is the line tMap[posX][posY] = oneTiles.getFrame(); Still, I can't figure out why
getFrame Method:
public function getFrame():void{
this.currentFrame;
}
Aucun commentaire:
Enregistrer un commentaire