Reputation: 171
I've ran into a problem in 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. I only get the error "Uncaught TypeError: Cannot set property 'x' of undefined" when I try to move by velocity. 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;
}
};
Upvotes: 0
Views: 380
Reputation: 212
Ok, here as an answer:
Velocity is a property of a physics body.
You should use:
this._player.body.velocity.x = 150
Upvotes: 2