玩家对象仅在 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;
})
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,在添加
player
之前调用update
函数(也称为在设置this.player
之前) 。由于
update
函数或多或少会在create
函数之后触发,因此场景的属性player
尚未设置。所以this.player
是undefined
,这就是错误的原因。解决方案是在访问
update
函数之前检查this.player
属性是否已设置。一旦设置了
this.player
属性,就会调用if
子句中的其他命令。或者像这样,最好阅读(遵循“返回早期模式”):
The issue is, that the
update
function is called, before aplayer
is added ( aka before thethis.player
is set).Since the
update
function will fire more or less right after thecreate
function, the propertyplayer
of the scene is not set yet. Sothis.player
isundefined
, which is the cause for the the error.The solution is to check, if the
this.player
property is set before accessing it, in theupdate
function.As soon as the
this.player
property is set, the other commands in theif
- clause are called.or like this, it is a bit better to read (following the "Return Early Pattern" ):