Я новичок в играх Phaser и не могу понять, как «вызвать» игрока в функции обновления, чтобы можно было перезапустить его позиции x и y.
Ошибка в строке 59: player.body.velocity.y = 0;
и, вероятно, это также не удается player.body.velocity.x = 0;
.
Я думаю, что это проблема глобальных переменных, какое-то решение?
Спасибо!
var Game = {};
Game.init = function(){
game.stage.disableVisibilityChange = true;
};
Game.preload = function() {
game.load.tilemap('map', 'assets/map/ejemplo4.json', null, Phaser.Tilemap.TILED_JSON);
game.load.spritesheet('tileset', 'assets/map/tilesheet.png',32,32);
game.load.image('sprite','assets/sprites/sprite.png');
};
var player;
var layer;
var map;
var blockedLayer;
Game.create = function(){
Game.playerMap = {};
game.physics.startSystem(Phaser.Physics.ARCADE);
var testKey = game.input.keyboard.addKey(Phaser.Keyboard.ENTER);
testKey.onDown.add(Client.sendTest, this);
map = game.add.tilemap('map');
map.addTilesetImage('tilesheet', 'tileset'); // tilesheet is the key of the tileset in map's JSON file
map.setCollisionBetween(1, 100);
layer = map.createLayer('backgroundLayer');
blockedLayer = map.createLayer('blockedLayer');
map.setCollisionBetween(1, 100000, true, 'blockedLayer');
layer.inputEnabled = true; // Allows clicking on the map ; it's enough to do it on the last layer
layer.events.onInputUp.add(Game.getCoordinates, this);
Client.askNewPlayer();
};
Game.getCoordinates = function(layer,pointer){
Client.sendClick(pointer.worldX,pointer.worldY);
};
Game.addNewPlayer = function(id,x,y){
player = game.add.sprite(x,y,'sprite');
//player.anchor.setTo(0.5, 0.5);
//player.scale.setTo(2, 2);
game.physics.arcade.enable(player);
player.body.collideWorldBounds = true;
Game.playerMap[id] = player;
};
Game.update = function(){
game.physics.arcade.collide(player, blockedLayer);
player.body.velocity.y = 0;
player.body.velocity.x = 0;
if(this.cursors.up.isDown) {
player.body.velocity.y -= 50;
}
else if(this.cursors.down.isDown) {
player.body.velocity.y += 50;
}
if(cursors.left.isDown) {
player.body.velocity.x -= 50;
}
else if(cursors.right.isDown) {
player.body.velocity.x += 50;
}
};
Game.movePlayer = function(id,x,y){
var player = Game.playerMap[id];
var distance = Phaser.Math.distance(player.x,player.y,x,y);
var tween = game.add.tween(player);
var duration = distance*10;
tween.to({x:x,y:y}, duration);
tween.start();
};
Game.removePlayer = function(id){
Game.playerMap[id].destroy();
delete Game.playerMap[id];
};