播放器对象仅在 update() 中未定义

player object undefined only in update()ƒ

不太确定发生了什么事。我需要为客户主要玩家物理体创建一个定义,我无法从 update() 读取它或它的任何属性,无论我在哪里或如何调用它。另外,如果这有点难以理解,我是一个年轻的新 JS 程序员,所以请多多关照。如果有人推荐更简单的东西,我愿意在需要时更改场景的构造。


    class MainScene extends Phaser.Scene {
    constructor() {
      super({ key: 'MainScene' })
    }

    preload() {
        this.load.image('grass','./assets/map.png',);
        this.load.spritesheet('graf', './assets/wang.png', { frameWidth: 200, frameHeight: 170})
      }
    
    create() {
      this.add.image(100,400,'grass');
      this.playerMap = {};
      Client.askNewPlayer();
      window.myScene = this;
      this.body  = this.matter.bodies.circle(
          1,
          1,
          10,
          {
              isSensor: true
          }
          );
    }
    addNewPlayer(id, x, y) {
        if(id == clientID){
            this.playerMap[id] = this.matter.add.sprite(x, y, 'graf','', {'shape' : this.body['player-20-00']}).setScale(.5);
            this.player = this.playerMap[id];
            this.cameras.main.centerOn(this.player.x, this.player.y)
        }
        else{
        this.playerMap[id] = this.add.sprite(x,y,'graf').setScale(.5);
        }
    }
    removePlayer(id){
        this.playerMap[id].destroy();
    delete this.playerMap[id];
    }
    movePlayer(id, x, y) {       
        // var player = this.playerMap[id];
        // var distance = Phaser.Math.Distance.Between(player.x,player.y,x,y);
        // var duration = distance*10;
        // var tween = this.add.tween(player);
        // tween.to({x:x,y:y}, duration);
        // tween.start();
        
    }
  
    update(){
       //haven't been able to access any variables Ive called from here
       this.cameras.main.centerOn(this.player.x, this.player.y)//causes error "can't read properties of undefined"
//replacing this.player.y or y with playerMap[a].x or y doesn't work either even though its accessible from everywhere else and === (equivalent) 
    }
}

这是客户端对象,我很确定这不会影响我的问题

var Client = {};
var clientID;
Client.socket = io.connect();
Client.askNewPlayer = function(){
    Client.socket.emit('newplayer');
}
Client.socket.on('newplayer',function(data){
    window.myScene.addNewPlayer(data.id,data.x,data.y);
});

Client.socket.on('allplayers',function(data){
    console.log(data);
    for(var i = 0; i < data.length; i++){
        window.myScene.addNewPlayer(data[i].id,data[i].x,data[i].y);
    }
});

Client.socket.on('remove',function(id){
    window.myScene.removePlayer(id);
});
Client.socket.on('move',function(data){
    window.myScene.movePlayer(data.id,data.x,data.y);
});
Client.socket.on('id',function(id){
    clientID = id;
})

问题是,在添加 player 之前调用了 update 函数 (也就是在设置 this.player 之前).

由于update函数或多或少会在create函数之后触发,所以场景的属性player还没有设置。所以 this.playerundefined,这是错误的原因。

解决方案是检查this.player 属性是否在访问它之前设置,在update函数中。

一旦设置了 this.player 属性,就会调用 if - 子句中的其他命令。

update(){
    if(this.player){
      this.cameras.main.centerOn(this.player.x, this.player.y);
      // ...
    }
}

或者像这样,阅读(遵循"Return Early Pattern"会更好一些:

update(){
    if(!this.player){
        return ;
    }
    this.cameras.main.centerOn(this.player.x, this.player.y);
    // ...
 }