POO par l'utilisation du prototype
Bonjour tout le monde,
Je suis actuellement dans le développement d'un petit jeu en javascript en utilisant le framework easeljs.
Je voudrais créer un objet "CShip1" que je pourrais réutiliser à l'infinie. Cet objet est hérité de createjs.BitmapAnimation du framwork.
Quand je l'instancie une fois, il n'a aucun problème. Quand je veux en instancier plusieurs, ils sont tous identique comme si toutes mes instances étaient des pointeur d'une variable unique.
Voici plusieurs heure que je cherche et sans résultats. Pouvez-vous me dire ce qu'il cloche dans mon code ?
Main :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
(function(){
var screen_width;
var screen_height;
this.ListShip = new Array();
this.init = function(){
this.canvas = document.getElementById("testCanvas");
this.ListShip.push(new CShip1(this, 1, 150,150,90))
this.ListShip.push(new CShip1(this, 2, 10,10,90))
startGame();
}
this.startGame = function () {
this.stage = new createjs.Stage(this.canvas);
this.stage.addChild(this.ListShip[0]);
this.stage.addChild(this.ListShip[1]);
(...)
}
this.tick = function () {
this.stage.update();
}
})(); |
Mon objet :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| (function (window) {
var CShip1 = function (Parent, Joueur_ID, x, y, degr) {
this.initialize(Parent, Joueur_ID, x, y, degr);
}
var p = CShip1.prototype = new createjs.BitmapAnimation();
p.tourcomplet = 2 * Math.PI;
p.Rad2Degr = 180 / Math.PI;
//comportement déplacement
p.Speed = 5;
p.RayonBracage = 10;
p.Cible = null;
p.BitmapAnimation_initialize = p.initialize;
p.BitmapAnimation_tick = p._tick;
p.initialize = function (Parent, Joueur_ID, x, y, degr) {
this._Parent = Parent;
this._joueurID = Joueur_ID;
(...)
this.BitmapAnimation_initialize(this.spriteSheetChips1);
(...)
}
p._tick = function() {
this.BitmapAnimation_tick();
// Avant tout, on demande ce que veut faire le vaiseau
this._IAMove();
p.x += p.Speed * Math.cos(p.rotation/ p.Rad2Degr);
p.y += p.Speed * Math.sin(p.rotation/ p.Rad2Degr);
}
(...)
window.CShip1 = CShip1;
} (window)); |
Merci d'avance !