未捕获的TypeError:无法读取未定义的属性"set" [英] Uncaught TypeError: Cannot read property 'set' of undefined
问题描述
我已经像这样构建了一个构造函数(并将其放在预加载函数上方)
I've made a constructor (and put it above preload function) like this
character = function(CharX,CharY,CharSpeed){
this.x = CharX ;
this.y = CharY;
this.speed = CharSpeed;
this.AddSpriteSheet = function (SprX,SprY,key) {
this.character = game.add.sprite(SprX,SprY,key);}
};
稍后我添加的创建功能
var Char1 = new character(game.world.width*0.5,game.world.height*0.5,5);
Char1.AddSpriteSheet(game.world.width*0.5,game.world.height*0.5,'character');
Char1.anchor.set(50,50);
控制台读取
未捕获的TypeError:无法读取未定义的属性'set'"
"Uncaught TypeError: Cannot read property 'set' of undefined"
我做错了什么?
使错误更明显
推荐答案
您正在制作一个自定义类来表示字符,但它不是Phaser Sprite,因此它没有任何方法/属性. Sprite会这样做,除非您自己定义它们.如果要创建自己的类来扩展Phaser.Sprite,建议您查看此示例.只是搜索相位器扩展精灵"也将帮助您找到其他资源.
You're making a custom class to represent a character, but it's not a Phaser Sprite, and therefore it doesn't have any of the methods/properties a Sprite does unless you define them yourself. If you want to create your own class to extend Phaser.Sprite, I suggest you look at this forum post and also this example. Just googling "phaser extend sprite" will help you find some other resources as well.
本质上,您需要执行以下操作:
Essentially, you need to do something like this:
function Character(game, x, y) {
Phaser.Sprite.call(this, game, x, y, 'sprite key');
// define other properties for your character
}
Character.prototype = Object.create(Phaser.Sprite.prototype);
Character.prototype.constructor = Character;
然后您将所有角色的方法添加到原型中.
And then you would add all of your Character's methods onto the prototype.
这篇关于未捕获的TypeError:无法读取未定义的属性"set"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!