dimanche 22 février 2015

Spawn enemies with differnt speeds

i want to make an vertical crolling shooter.


right now it works but i only have one image that is called enemy.png


But i want so its random so enemy.png enemy1.png enemy2.png enemy3.png enemy4.png spawn random at the top of the screen.


and all these planes need to have different speeds so like enemy 2 needs to go faster then enemy4 for example.


But how do i do that


right now i have this script for my enemy's



//enemies
var enemy;
var enemyTotal = 5;
var enemies = [];
var enemy_x = 50;
var enemy_y = -45;
var enemy_w = 50;
var enemy_h = 50;
var speed = 3;


//Build enemy array of x/y coordinate, width, height, and speed
for (var i = 0; i < enemyTotal; i++) {
enemies.push([enemy_x, enemy_y, enemy_w, enemy_h, speed]);
enemy_x += enemy_w + 60;
}

//Iterate through array of enemies and draw them on the canvas
function drawEnemies() {
for (var i = 0; i < enemies.length; i++) {
ctx.drawImage(enemy, enemies[i][0], enemies[i][1]);
}
}

//Iterate through array of enemies and update their position
function moveEnemies() {
for (var i = 0; i < enemies.length; i++) {
if (enemies[i][1] < height) {
enemies[i][1] += enemies[i][4];
} else if (enemies[i][1] > height - 1) {
//after the enemies scroll off screen, this value is where they get re-spawned
enemies[i][1] = -45;
}
}
}

//Check for laser collision
function hitTest() {
var remove = false;
for (var i = 0; i < lasers.length; i++) {
for (var j = 0; j < enemies.length; j++) {
//laser's y position is less than or equal to the enemy's y position plus its height -AND-
//laser's x position is greater than or equal to the enemy's x position -AND-
//laser's x position is less than or equal to the enemy's x position plus it's width
if (lasers[i][1] <= (enemies[j][1] + enemies[j][3]) && lasers[i][0] >= enemies[j][0] && lasers[i][0] <= (enemies[j][0] + enemies[j][2])) {
remove = true;
enemies.splice(j, 1); //enemy at this array position no longer gets drawn
enemies.push([(Math.random() * 500) + 50, -45, enemy_w, enemy_h, speed]); //refill a new enemy in a random x position between 50 and 500
}
}
if (remove == true) {
lasers.splice(i, 1); //laser at this array position no longer gets drawn
remove = false;
score += 10;
}
}
}


Hope you guys can help


Aucun commentaire:

Enregistrer un commentaire