2017-07-13 35 views
-1

“设置”我做了一个构造函数(并把它上面的预载功能)这样遗漏的类型错误:无法读取属性未定义

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:无法读取未定义的属性'set' 我做了什么错?

+0

您永远不会添加任何名为'anchor'的属性 –

回答

0

您正在创建一个自定义类来表示一个字符,但它不是一个Phaser Sprite,因此它没有任何Sprite所执行的任何方法/属性,除非您自己定义它们。如果你想创建自己的类来扩展Phaser.Sprite,我建议你看看this forum postthis example。只要使用Google搜索“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; 

然后你会增加你的所有人物的方法到原型。

0

您的构造函数character没有属性anchor,因此Char1.anchor不存在,Char1.anchor.set也不存在。

0

'设置' 的未定义” BCA “中设置” 是陂溪的比较 “setTo” 移相器的

Char1.anchor.set(50,50); 替代由 Char1.anchor.setTo(0.5); // 0.5,0.5将指向精灵广场的中心,如果是这样的意图 这也是真理.scale.setTo();

此外,如果你创建新的原型对象出创建功能我会建议遵循此示例https://phaser.io/examples/v2/games/tanks

相关问题