我正在使用 Phaser 框架,我想创建一个新类来保存 Phaser 中 Sprite 类的所有属性,所以我尝试这样做
var mario = function(){
Phaser.Sprite.call(this); // inherit from sprite
};
但是出现错误“未捕获的类型错误:未定义不是函数”
然后我试过了
var mario = function(){
this.anything = "";
};
mario.prototype = new Phaser.Sprite();
好的,它可以工作,但它调用了移相器构造函数,我不想在执行
var heroObj = new mario();
之前创建 Sprite 我应该怎么办 ?
最佳答案
像这样尝试。我添加了一个命名空间来避免全局变量。
var YourGameNSpace = YourGameNSpace || {};
YourGameNSpace.Mario = function (game) {
'use strict';
Phaser.Sprite.call(this, game);
};
// add a new object Phaser.Sprite as prototype of Mario
YourGameNSpace.Mario.prototype = Object.create(Phaser.Sprite.prototype);
// specify the constructor
YourGameNSpace.Mario.constructor = YourGameNSpace.Mario;
//add new method
YourGameNSpace.Mario.prototype.init = function () {
'use strict';
...
};
在你可以实例化它之后:
var mario = new YourGameNSpace.Mario(game);
关于javascript - 扩展 Phaser.js 类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29231281/