Hey everyone!
I've been working on creating a dodge game using JavaScript.
I have a question about how to ensure that each of the 3 enemies I create will have unique functions. Right now, they all move in the same direction determined by enemyrandom1(). How can I make them separate and independent?
Here's the code I'm using to create the enemies:
var item;
var enemy;
var enemys = new Array();
function createEnemy(){
enemy = new createjs.Shape();
enemy.graphics.beginFill("black").drawRect(0, 0, 10, 10);
enemy.x = (Math.random() * 400) + 1;
enemy.y = 50;
stage.addChild(enemy);
enemys.push(enemy);
console.log(enemys);
}
createEnemy();
createEnemy();
createEnemy();
function enemyforloop(){
for(var i=0; i<enemys.length; i++) {
item = enemys[i];
enemyMove();
}
}
The enemyforloop is included in the Ticker method. The function enemyMove() is responsible for moving the enemies and placing them in random positions once they reach a certain point.
function enemyMove(){
if(enemyrandom == 1){
item.y -= enemyspeed;
if(item.y < -10){
enemyrandom1();
}
}
}
function enemyrandom1(){
enemyrandom = Math.floor((Math.random() * 4) + 1);
enemyspeed = Math.random() * 2 + 1;
if(enemyrandom == 1){
item.y = 300;
item.x = Math.floor((Math.random() * 400) + 1);
}
}
I didn't include enemyrandom and enemymove 2, 3, 4 to keep it concise. I hope my question was clear enough.
You can see the current version of the game here: http://jsfiddle.net/HqYeD/411/