玩家对象仅在 update() 中未定义ƒ

发布于 2025-01-09 20:09:59 字数 2681 浏览 1 评论 0原文

不太确定该怎么称呼发生了什么。我需要为客户端主要玩家物理主体创建一个定义,但无论我在何处或如何从 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;
})

not exactly sure what to call whats going on. I need to create a definition for the clients main player physics body and i can't read it or any of its properties, no matter where or how i call it, from update(). Also if this is a bit hard to understand Im a young and new JS programmer so bare with me. Im willing to change the construction of my scenes if i need to if anyone recommends something easier.


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

here is the client object, I'm pretty sure this doesn't affect my problem

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俏︾媚 2025-01-16 20:09:59

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

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

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

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

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

或者像这样,最好阅读(遵循“返回早期模式”

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

The issue is, that the update function is called, before a player is added ( aka before the this.player is set).

Since the update function will fire more or less right after the create function, the property player of the scene is not set yet. So this.player is undefined, which is the cause for the the error.

The solution is to check, if the this.player property is set before accessing it, in the update function.

As soon as the this.player property is set, the other commands in the if - clause are called.

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

or like this, it is a bit better to read (following the "Return Early Pattern" ):

update(){
    if(!this.player){
        return ;
    }
    this.cameras.main.centerOn(this.player.x, this.player.y);
    // ...
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文