私の状況では、Box2DWeb を使用してキャンバス ゲームに取り組んでいるので、キャンバスに画像を描画するSpriteというクラスがあり、 Spriteからメソッドとプロパティを継承するPhysicsSpriteクラスを作成したいと考えています。
私のスプライトクラス(Sprite.js):
Sprite = function (position, dimension, image) {
this.Position = position;
this.Dimension = dimension;
if (image)
this.Image = image;
...
this.Draw = function (g) {
...
}
...
};
そして、私はPhysicsSprite (PhysicsSprite.js) でプロトタイプを次のように設定しています:
PhysicsSprite = function(position, dimension, world) {
this.prototype = new Sprite(position, dimension, undefined);
//alert(this.prototype.Position.X)
var body = CreateBody();
this.GetBody = function () { return body; }
this.Draw = function (g) {
...
}
function CreateBody () {
...
}
};
位置を警告するalert(this.prototype.Position.X)に注意してください。ただし、コードをthis.Position.Xに変更するとエラーが発生します。同様に、PhysicsSpriteのインスタンスがある場合は、プロトタイプを明示的に呼び出す必要があります。
これにより、オブジェクトのプロトタイプを設定しておらず、単に というプロパティを作成し、prototype
それを新しいSpriteに設定しただけだと思いました。
誰かが私を助けて、私が間違っていることを説明できれば、それは大歓迎です。私は失読症なので、いつも変数のスペルを間違えてイライラするので、最初にそれを探しましたが、すべて問題ないように見えました。