Phaser:'无法读取'undefined'属性'body'

时间:2018-05-27 10:02:00

标签: javascript html phaser-framework

我是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];
    };

1 个答案:

答案 0 :(得分:0)

解决!

在更新功能中,我检查游戏中是否存在精灵,因此代码为:

if (typeof player !== "undefined"){

  player.body.velocity.y = 0;
  player.body.velocity.x = 0;

  if (cursors.left.isDown) {
    player.body.velocity.x = -200;
  } else if (cursors.right.isDown) {
    player.body.velocity.x = 200;
  } else if (cursors.up.isDown) {
    player.body.velocity.y = -200;    
  } else if (cursors.down.isDown) {
    player.body.velocity.y = 200;
  }
}

感谢您的帮助!