[英]Phaser: Cannot Access Variable Defined in Other Scope
I've ran into a problem in Phaser. 我在Phaser中遇到了一个问题。 Basically, the player variable defined in the main schoolyard function isn't accesible.
基本上,在主要校园功能中定义的玩家变量是不可访问的。 I first tried to define it together with the other variables, but since that didn't work I tried defining it in the main function.
我首先尝试将其与其他变量一起定义,但是由于这种方法不起作用,因此我尝试在main函数中对其进行定义。 I only get the error "Uncaught TypeError: Cannot set property 'x' of undefined" when I try to move by velocity.
尝试按速度移动时,仅出现错误“未捕获的TypeError:无法设置未定义的属性'x'”。 I hope someone can help, thanks in advance.
希望有人能提供帮助,在此先感谢。
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;
}
};
Ok, here as an answer: 好的,这里是一个答案:
Velocity is a property of a physics body. 速度是物理物体的属性。
You should use: 您应该使用:
this._player.body.velocity.x = 150
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.