扩展 pixi.js在构造函数中对访问父的属性进行精灵化

Extend pixi.js sprite an accessing parent's properties in constructor

本文关键字:属性 精灵 访问 js pixi 构造函数 扩展      更新时间:2023-09-26

我正在尝试扩展 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;

但是,我不断收到错误"未捕获的类型错误:无法设置未定义的属性'x'"

this.position.x = window.width/2;

我相信在 Object.create() 调用之前我无法访问父类的属性......那么正确的方法是什么呢?

Ship.prototype = Object.create(PIXI.精灵原型);

应该做这个伎俩!