扩展 pixi.js sprite 并在构造函数中访问 parent 的属性
Extend pixi.js sprite an accessing parent's properties in constructor
我正在尝试扩展 pixi.js' sprite class 并在我的 child class 构造函数中将其设置为 position.x 属性
var Ship = function(x, y, image, focused) {
PIXI.Sprite.fromImage.call(this, image);
this.x = x;
this.y = y;
this.image = image;
this.focused = typeof focused != 'undefined' ? focused : false;
this.position.x = window.width/2;
};
Ship.prototype = Object.create(PIXI.Sprite.fromImage.prototype);
Ship.prototype.constructor = Ship;
但是,我在
上不断收到错误 "Uncaught TypeError: Cannot set property 'x' of undefined"
this.position.x = window.width/2;
我相信在 Object.create() 调用之前我无法访问 parent class 的属性...那么正确的做法是什么?
Ship.prototype = Object.create(PIXI.Sprite.prototype);
应该成功!
我正在尝试扩展 pixi.js' sprite class 并在我的 child class 构造函数中将其设置为 position.x 属性
var Ship = function(x, y, image, focused) {
PIXI.Sprite.fromImage.call(this, image);
this.x = x;
this.y = y;
this.image = image;
this.focused = typeof focused != 'undefined' ? focused : false;
this.position.x = window.width/2;
};
Ship.prototype = Object.create(PIXI.Sprite.fromImage.prototype);
Ship.prototype.constructor = Ship;
但是,我在
上不断收到错误 "Uncaught TypeError: Cannot set property 'x' of undefined"this.position.x = window.width/2;
我相信在 Object.create() 调用之前我无法访问 parent class 的属性...那么正确的做法是什么?
Ship.prototype = Object.create(PIXI.Sprite.prototype);
应该成功!