我正在使用 Phaser 框架,我想创建一个新类来保存 Phaser 中 sprite 类的所有属性,所以我尝试这样做
var mario = function(){
Phaser.Sprite.call(this); // inherit from sprite
};
但是出现错误“Uncaught TypeError: undefined is not a function”
然后我试了
var mario = function(){
this.anything = "";
};
mario.prototype = new Phaser.Sprite();
好的,它可以工作,但它调用了移相器构造函数,我不想创建 Sprite ,直到我执行 var heroObj = new mario();
我该怎么办?
最佳答案
这样试试。我添加了一个命名空间以避免全局变量。
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/