Uncaught TypeError: Cannot read property 'set' of undefined
Uncaught TypeError: Cannot read property 'set' of undefined
我做了一个像这样的构造函数(并将其放在预加载函数之上)
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);
控制台读取
"Uncaught TypeError: Cannot read property 'set' of undefined"
我做错了什么?
编辑:使错误更明显
您的构造函数 character
没有 属性 anchor
,因此 Char1.anchor
不存在,Char1.anchor.set
.[=14 也不存在=]
您正在自定义 class 来表示角色,但它不是 Phaser Sprite,因此它没有任何 methods/properties Sprite 所具有的功能,除非您定义它们你自己。如果您想创建自己的 class 来扩展 Phaser.Sprite,我建议您查看 this forum post and also this example。谷歌搜索 "phaser extend sprite" 也可以帮助您找到其他一些资源。
基本上,你需要做这样的事情:
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;
然后您可以将角色的所有方法添加到原型中。
'set' of undefined" bca "set" 是 Pixi 与 "setTo" Phaser 的
的比较
Char1.anchor.set(50,50);
替换为
Char1.anchor.setTo(0.5);// 0.5, 0.5 将指向精灵正方形的中心,如果这是意图的话。
这也是 .scale.setTo();
的真理
另外,如果您使用 create 函数创建新的原型对象,我建议您遵循此示例 https://phaser.io/examples/v2/games/tanks
我做了一个像这样的构造函数(并将其放在预加载函数之上)
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);
控制台读取
"Uncaught TypeError: Cannot read property 'set' of undefined"
我做错了什么?
编辑:使错误更明显
您的构造函数 character
没有 属性 anchor
,因此 Char1.anchor
不存在,Char1.anchor.set
.[=14 也不存在=]
您正在自定义 class 来表示角色,但它不是 Phaser Sprite,因此它没有任何 methods/properties Sprite 所具有的功能,除非您定义它们你自己。如果您想创建自己的 class 来扩展 Phaser.Sprite,我建议您查看 this forum post and also this example。谷歌搜索 "phaser extend sprite" 也可以帮助您找到其他一些资源。
基本上,你需要做这样的事情:
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;
然后您可以将角色的所有方法添加到原型中。
'set' of undefined" bca "set" 是 Pixi 与 "setTo" Phaser 的
的比较Char1.anchor.set(50,50); 替换为 Char1.anchor.setTo(0.5);// 0.5, 0.5 将指向精灵正方形的中心,如果这是意图的话。 这也是 .scale.setTo();
的真理另外,如果您使用 create 函数创建新的原型对象,我建议您遵循此示例 https://phaser.io/examples/v2/games/tanks