I am a flash developer, and I am building a Tower Defense game. The world is being built through tiles, and I have gotten that accomplished easily. I have also gotten level changes and enemy spawning down as well. However, I wish the player to be able to spawn turrets, and have those turrets be on specific tiles, based upon where the player placed it.
Here is my code:
stop();
colOffset = 50;
rowOffset = 50;
guns = [];
placed = true;
dead = 0;
spawned = 0;
level = 1;
interval = 350 / level;
amount = level * 20;
counter = 0;
numCol = 14;
numRow = 10;
tiles = [];
k = 0;
create = false;
tileName = new Array("road","grass","end", "start");
board = new Array(
new Array(1,1,1,1,3,1,1,1,1,1,2,1,1,1),
new Array(1,1,1,0,0,1,1,1,1,1,0,1,1,1),
new Array(1,1,1,0,1,1,1,1,1,1,0,0,1,1),
new Array(1,1,1,0,0,0,1,1,1,1,1,0,1,1),
new Array(1,1,1,0,1,0,0,0,1,1,1,0,0,1),
new Array(1,1,1,0,1,1,1,0,0,1,1,1,0,1),
new Array(1,1,0,0,1,1,1,1,0,1,1,0,0,1),
new Array(1,1,0,1,1,1,1,1,0,1,0,0,1,1),
new Array(1,1,0,0,0,0,0,0,0,1,0,1,1,1),
new Array(1,1,1,1,1,1,1,1,0,0,0,1,1,1)
);
buildBoard();
function buildBoard(){
for ( col = 0; col < numCol; col++){
for ( row = 0; row < numRow; row++){
_root.attachMovie("tile", "tile_" + col + "_" + row, _root.getNextHighestDepth());
theTile = eval("tile_" + col + "_" + row);
theTile._x = (col * 50);
theTile._y = (row * 50);
theTile.row = row;
theTile.col = col;
tileType = board[row][col];
theTile.gotoAndStop(tileName[tileType]);
tiles.push(theTile);
}
}
}
init();
function init(){
onEnterFrame = function(){
counter += 1;
if ( spawned < amount && counter > 50){
min= _root.attachMovie("minion","minion",_root.getNextHighestDepth());
min._x = tile_4_0._x + 25;
min._y = tile_4_0._y + 25;
min.health = 100;
choose = Math.round(Math.random());
if ( choose == 0 ){
min.waypointX = [ tile_4_1._x +25, tile_3_1._x + 25, tile_3_2._x + 25, tile_3_6._x + 25, tile_2_6._x + 25, tile_2_8._x + 25, tile_8_8._x + 25, tile_8_9._x + 25, tile_10_9._x + 25, tile_10_7._x + 25, tile_11_7._x + 25, tile_11_6._x + 25, tile_12_6._x + 25, tile_12_4._x + 25, tile_11_4._x + 25, tile_11_2._x + 25, tile_10_2._x + 25, tile_10_0._x + 25];
min.waypointY = [ tile_4_1._y +25, tile_3_1._y + 25, tile_3_2._y + 25, tile_3_6._y + 25, tile_2_6._y + 25, tile_2_8._y + 25, tile_8_8._y + 25, tile_8_9._y + 25, tile_10_9._y + 25, tile_10_7._y + 25, tile_11_7._y + 25, tile_11_6._y + 25, tile_12_6._y + 25, tile_12_4._y + 25, tile_11_4._y + 25, tile_11_2._y + 25, tile_10_2._y + 25, tile_10_0._y + 25];
}
else if ( choose == 1 ){
min.waypointX = [ tile_4_1._x +25, tile_3_1._x + 25, tile_3_2._x + 25, tile_3_3._x + 25, tile_5_3._x + 25, tile_5_4._x + 25, tile_7_4._x + 25, tile_7_5._x + 25, tile_8_5._x + 25, tile_8_8._x + 25, tile_8_9._x + 25, tile_10_9._x + 25, tile_10_7._x + 25, tile_11_7._x + 25, tile_11_6._x + 25, tile_12_6._x + 25, tile_12_4._x + 25, tile_11_4._x + 25, tile_11_2._x + 25, tile_10_2._x + 25, tile_10_0._x + 25 ];
min.waypointY = [ tile_4_1._y +25, tile_3_1._y + 25, tile_3_2._y + 25, tile_3_3._y + 25, tile_5_3._y + 25, tile_5_4._y + 25, tile_7_4._y + 25, tile_7_5._y + 25, tile_8_5._y + 25, tile_8_8._y + 25, tile_8_9._y + 25, tile_10_9._y + 25, tile_10_7._y + 25, tile_11_7._y + 25, tile_11_6._y + 25, tile_12_6._y + 25, tile_12_4._y + 25, tile_11_4._y + 25, tile_11_2._y + 25, tile_10_2._y + 25, tile_10_0._y + 25 ];
}
min.i = 0;
counter = 0;
spawned += 1;
min.onEnterFrame = function(){
dx = this.waypointX[this.i] - this._x;
dy = this.waypointY[this.i] - this._y;
radians = Math.atan2(dy,dx);
degrees = radians * 180 / Math.PI;
xspeed = Math.cos(radians);
yspeed = Math.sin(radians);
this._x += xspeed;
this._y += yspeed;
if( this._x == this.waypointX[this.i] && this._y == this.waypointY[this.i]){
this.i++;
}
if ( this._x == tile_10_0._x + 25 && this._y == tile_10_0._y + 25){
this.removeMovieClip();
dead += 1;
}
}
}
if ( dead >= amount ){
dead = 0;
level += 1;
amount = level * 20;
spawned = 0;
}
}
btnM.onRelease = function(){
create = true;
}
}
game.onEnterFrame = function(){
}
It is possible for me however to complete this task, but only once. I am able to make the turret, drag it over to a tile, and have it attach itself to the tile. No problem. The issue is, I cannot do these multiple times. Please Help.