no estoy exactamente seguro de cómo llamar a lo que está pasando. Necesito crear una definición para el cuerpo físico del jugador principal del cliente y no puedo leerlo ni ninguna de sus propiedades, sin importar dónde o cómo lo llame, desde update(). Además, si esto es un poco difícil de entender, soy un joven y nuevo programador de JS, así que sé sincero conmigo. Estoy dispuesto a cambiar la construcción de mis escenas si es necesario si alguien recomienda algo más fácil.
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) } }aquí está el objeto del cliente, estoy bastante seguro de que esto no afecta mi problema
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; })El problema es que se llama a la función de update antes de agregar un player (es decir, antes de configurar this.player ) .
Dado que la función de update se activará más o menos justo después de la función de create , la propiedad del player de la escena aún no está configurada. Entonces this.player no está undefined , que es la causa del error.
La solución es verificar, si la propiedad this.player está configurada antes de acceder a ella, en la función de update .
Tan pronto como se establece la propiedad this.player , se llaman los otros comandos en la cláusula if -.
update(){ if(this.player){ this.cameras.main.centerOn(this.player.x, this.player.y); // ... } }o así, es un poco mejor leer (siguiendo el "Patrón de retorno temprano" ) :
update(){ if(!this.player){ return ; } this.cameras.main.centerOn(this.player.x, this.player.y); // ... }