Kyle Michiels
Kyle Michiels

Reputation: 25

Putting objects in an Array

I am trying to make a game using Kinetic JS and I want to have an 'infinte' amout of Enemies. I am trying to do this using an array.

My code for preloading:

enemy = new Kinetic.Image({x:10,y:10,image: enemyImage});
enemies.push(enemy);
enemies.push(enemy);

And for reffrencing to them in my level code:

    function start(){

gameObjectsLayer.removeChildren();

gameObjectsLayer.add(background);
gameObjectsLayer.add(ship);
gameObjectsLayer.add(enemies[0]);
gameObjectsLayer.add(enemies[1]);




gameObjectsLayer.draw();

switchGameState(GAME_STATE_LEVEL_1);
}



function level() {
    gameLoop=setInterval(update,20);  

}

function update(){

enemies[0].setY(100);
enemies[1].setY(300);
}

But I only can see one enemy.

How do I use this properly?

I am new to the kinetic JS system.

Smoothy,

Upvotes: 0

Views: 115

Answers (1)

Quentin
Quentin

Reputation: 943615

You are creating one enemy, then you are putting two references to it in the array.

You need to create two enemies in the first place.

enemies.push(new Kinetic.Image({x:10,y:10,image: enemyImage}));
enemies.push(new Kinetic.Image({x:10,y:10,image: enemyImage}));

Upvotes: 1

Related Questions