我在Phaser遇到了问题。基本上,主校园功能中定义的玩家变量是不可访问的。我首先尝试将其与其他变量一起定义,但由于这不起作用,我尝试在main函数中定义它。我只得到错误" Uncaught TypeError:无法设置属性' x'未定义"当我试图按速度移动时我希望有人能提前帮助,谢谢。
var Schoolyard = function() {
this._player = null;
};
var map;
var backgroundLayer;
var backgroundLayer2;
var collisionLayer;
var cursors;
Schoolyard.prototype = {
preload: function() {
},
create: function() {
this.game.physics.startSystem(Phaser.Physics.ARCADE);
map = this.game.add.tilemap('schoolyard');
map.addTilesetImage('tiles');
map.addTilesetImage('tiles2');
backgroundLayer = map.createLayer('BackgroundLayer');
brackgroundLayer2 = map.createLayer('BackgroundLayer2');
collisionLayer = map.createLayer('CollisionLayer');
this._player = this.game.add.sprite(400,400,'main');
this.game.physics.enable(this._player);
this.game.camera.follow(this._player);
this._player.frame = 30;
cursors = this.game.input.keyboard.createCursorKeys();
},
update: function() {
if (cursors.right.isDown)
this._player.velocity.x = 150;
}
};
答案 0 :(得分:2)
好的,这里作为答案:
速度是物理机构的属性。
您应该使用:
this._player.body.velocity.x = 150