我似乎无法使用自定义图块对象在 Phaser 中访问对象的原型方法。我究竟做错了什么?

I can't seem to access a object's prototype method in Phaser with a custom tile object. What am I doing wrong?

我正在扩展 Phaser 中的基本 Sprite 对象以包含一些附加功能,主要是使用快捷方式切换纹理的功能。但是,当我尝试访问该方法时,我得到

"hex.switchSprite is not a function"

经过一些基础研究,大多数答案都说要包含 .prototype - 然而..它已经在方法中了。这是我目前拥有的对象本身。出于测试目的,switchSprite 中的所有内容均已删除:

hexTile = function(game, x, y)
{
    Phaser.Sprite.call(this, game, x, y, 'masterTileSheet', 'Gemstone_Missing');
    this.scale.x = 0.75;
    this.scale.y = 0.75;
    this.anchor.x = 0.5;
    this.anchor.y = 0.5;
}

hexTile.prototype.switchSprite = function(name)
{

}

hexTile.prototype = Object.create(Phaser.Sprite.prototype);
hexTile.prototype.constructor = hexTile;

我遵循了 Phaser 文档中列出的示例,当我不使用 switchSprite 时,它​​起作用了:

var hex = new hexTile(game, 0, 0);
var point = calculateGrid(x,y);
hex.x = point.x;
hex.y = point.y;

gameContainer.add(hex);

//console.log(returnRandomHex());

hex.switchSprite();

以上是我访问的方式 运行 - 它应该是正确的,但是对于 switchSprite 我只是得到上面的错误。

一个观察:你正在覆盖你的原型:

hexTile.prototype.switchSprite = function(name)
{

}

hexTile.prototype = Object.create(Phaser.Sprite.prototype);

您在 添加了 switchSprite 函数后设置了 hexTile prototype。试试调换这两个语句的顺序。