我正在尝试扩展 pixi.js 的 sprite 类并在我的子类构造函数中设置它的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:无法设置未定义的属性 'x'”
this.position.x = window.width/2;
我相信在调用 Object.create() 之前我无法访问父类的属性...那么执行此操作的正确方法是什么?
最佳答案
Ship.prototype = Object.create(PIXI.Sprite.prototype);
应该可以解决问题!
关于javascript - 扩展 pixi.js sprite 并在构造函数中访问父级的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28140597/