【问题标题】:Phaser : 'Cannot read property 'body' of undefined'Phaser:'无法读取未定义的属性'body''
【发布时间】:2018-11-06 03:21:58
【问题描述】:

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

【问题讨论】:

  • 变量player 超出了您的范围,您在preload 上定义它并尝试在update 上使用它,这就是它未定义的原因。也许您可以将它们移到文件顶部,而不是 Phaser 专家
  • 如果我没记错的话,它是在“预加载”之外定义的。即便如此,我试图把它放在文档的顶部,它仍然不起作用:S
  • 你是对的,也许addNewPlayer 永远不会被调用?
  • Addnewplayer 在另一个类中被调用(我正在使用套接字进行多人游戏),玩家被添加,它移动,但我无法重新启动 'body.velocity'

标签: javascript html phaser-framework


【解决方案1】:

解决了!

在更新函数中,我检查游戏中是否存在精灵,所以代码是:

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;
  }
}

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2020-10-30
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多