服务器游戏类吸引方法未在客户端的扩展子类上定义
我有一个state.js文件,用于socket.io文件的后端。我已经为所有玩家定义了一个列表,player
class和一个扩展的cartare
类,其中我从player
类中继承了所有方法。
以下是我在服务器端的类玩家的代码:
class Player {
constructor(x,y){
this.x = x;
this.y = y;
this.radius = 20;
this.xVel = 4;
this.yVel = 4;
this.usingSuper = false;
this.superLeft = 3;
this.immune = false;
this.shield = false;
this.duration = 3000;
this.cooldown = 5000;
}
draw() {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2);
ctx.fill();
ctx.closePath();
}
这是我的扩展类“ max”的代码:
class Max extends Player { // superspeed character
constructor(x,y){
super(x,y);
this.color = 'yellow';
}
useSuper(){
this.superLeft --;
this.usingSuper = true;
this.xVel = 15;
this.yVel = 15;
// 5 lines below turns the player color to white for a split second
let originalColor = this.color;
this.color = 'white';
setTimeout(()=>{
this.color = originalColor;
}, 500);
setTimeout(()=>{
this.xVel = 4;
this.yVel = 4;
setTimeout(()=>{
this.usingSuper = false;
}, this.cooldown);
}, this.duration);
}
这是我在main.js中调用client端的draw函数的
socket.on('state', (gameState)=>{
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let player in gameState.players){
gameState.players[player].draw();
}
})
代码。它在浏览器控制台中给了我这个错误:
undureck typeError:gamestate.players [player]。绘图不是函数 在插座。 (main.js:13:35)在socket.emitter.emit上 (index.mjs:136:20)在socket.emitevent(socket.js:278:20) socket.onevent(socket.js:265:18)at socket.onpacket (socket.js:235:22)at manager.emitter.emit(index.mjs:136:20)at exoder.emitter.emit (index.mjs:136:20)在解码器(index.js:124:17)at manager.ondata (经理JS:192:22)
即使我直接在子类max
中直接覆盖它,绘制功能仍然不确定。如何解决此问题?
I have a state.js file for the back-end of socket.io files. I have defined a list for all the Players, Player
class, and an extended Character
class where I inherited all the methods from the Player
class.
Below is my code for the class Player on the server side:
class Player {
constructor(x,y){
this.x = x;
this.y = y;
this.radius = 20;
this.xVel = 4;
this.yVel = 4;
this.usingSuper = false;
this.superLeft = 3;
this.immune = false;
this.shield = false;
this.duration = 3000;
this.cooldown = 5000;
}
draw() {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2);
ctx.fill();
ctx.closePath();
}
Here is my code for the extended class "Max":
class Max extends Player { // superspeed character
constructor(x,y){
super(x,y);
this.color = 'yellow';
}
useSuper(){
this.superLeft --;
this.usingSuper = true;
this.xVel = 15;
this.yVel = 15;
// 5 lines below turns the player color to white for a split second
let originalColor = this.color;
this.color = 'white';
setTimeout(()=>{
this.color = originalColor;
}, 500);
setTimeout(()=>{
this.xVel = 4;
this.yVel = 4;
setTimeout(()=>{
this.usingSuper = false;
}, this.cooldown);
}, this.duration);
}
And this is when I call the draw function on the client-side in the main.js
socket.on('state', (gameState)=>{
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let player in gameState.players){
gameState.players[player].draw();
}
})
When I run this code, it gives me this error in the browser console:
Uncaught TypeError: gameState.players[player].draw is not a function
at Socket. (main.js:13:35) at Socket.Emitter.emit
(index.mjs:136:20) at Socket.emitEvent (socket.js:278:20) at
Socket.onevent (socket.js:265:18) at Socket.onpacket
(socket.js:235:22) at Manager.Emitter.emit (index.mjs:136:20) at
Manager.ondecoded (manager.js:200:14) at Decoder.Emitter.emit
(index.mjs:136:20) at Decoder.add (index.js:124:17) at Manager.ondata
(manager.js:192:22)
The draw function is still undefined even when I directly override it in the subclass Max
. How can I fix this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论