I got the following "Enemy" Class in JavaScript:
function Enemy(position, rotation) {
this.name = null;
this.enemyForm = new Kinetic.Rect({
x: 0,
y: 0,
width: 20,
height: 20,
fill: 'red',
stroke: 'black',
strokeWidth: 1
});
this.setX(position.posX);
this.setY(position.posY);
this.setRotation(rotation);
this.setOffset(10, 10);
this.add(this.enemyForm);
}
Enemy.prototype = new Kinetic.Group();
As you can see, i extend a Kinetic.Group for that because the Enemies will have some more Kinetic Elements and not just a Rectangle.
Now i create some Instances of that "Class" and add them to the game layer:
var enemy1 = new Enemy({posX: 50, posY: 50}, 0);
this.layer.add(enemy1);
var enemy2 = new Enemy({posX: 100, posY: 100}, 0);
this.layer.add(enemy2);
var enemy3 = new Enemy({posX: 200, posY: 200}, 0);
this.layer.add(enemy3);
The problem: Every Enemy gets the position of "enemy3", and not their own. So, every Enemy will be drawn at position "200, 200". Now if i try this without inheritance, it works fine:
function Enemy(position, rotation) {
this.name = null;
this.enemyForm = new Kinetic.Group();
var rect = new Kinetic.Rect({
x: 0,
y: 0,
width: 20,
height: 20,
fill: 'red',
stroke: 'black',
strokeWidth: 1
});
this.enemyForm.setX(position.posX);
this.enemyForm.setY(position.posY);
this.enemyForm.setRotation(rotation);
this.enemyForm.setOffset(10, 10);
this.enemyForm.add(rect);
}
Can anyone tell me what i am missing, and why i don´t get separate objects with the first method?