Phaser:无法访问在其他范围内定义的变量
Phaser: Cannot Access Variable Defined in Other Scope
我 运行 在 Phaser 中遇到了问题。基本上,主校园函数中定义的玩家变量是不可访问的。我首先尝试将它与其他变量一起定义,但由于这不起作用,我尝试在主函数中定义它。当我尝试以速度移动时,我只会收到错误 "Uncaught TypeError: Cannot set property 'x' of undefined"。希望有人能帮忙,先谢谢了。
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;
}
};
好的,这里作为答案:
速度是物理体的属性。
你应该使用:
this._player.body.velocity.x = 150
我 运行 在 Phaser 中遇到了问题。基本上,主校园函数中定义的玩家变量是不可访问的。我首先尝试将它与其他变量一起定义,但由于这不起作用,我尝试在主函数中定义它。当我尝试以速度移动时,我只会收到错误 "Uncaught TypeError: Cannot set property 'x' of undefined"。希望有人能帮忙,先谢谢了。
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;
}
};
好的,这里作为答案:
速度是物理体的属性。
你应该使用:
this._player.body.velocity.x = 150