I am working on a game that sends backend server objects to frontend clients and then displays the data and then sends back user input back to the server. I am having trouble because when I call openRightGui
and run this.setState()
from my React Game
class, it makes it so other calls to this.setState()
error. Can someone please tell me my stupid error : )
Here is my react game element:
import './game.css';
import React from 'react';
import socket from 'socket.io-client';
import game_grid from './grid_image.jpg';
// Import Enemies
import Frie from './Enemies/Frie.js';
function centerX(grid_x){
return (((grid_x-10)*-1) * 5).toString() + "%";
}
function centerY(grid_y){
return (((grid_y-10)*-1) * 5 * 1.6).toString() + "%";
}
function getEnemy(enemy){
switch(enemy.type){
case 'Frie':
return (<Frie x={enemy.x} y={enemy.y}/>)
default:
return '';
}
}
class Game extends React.Component {
constructor(props){
super(props);
let websocket;
if (window.location.href.slice(0, 10) === 'http://loc'){
websocket = socket("http://localhost:2999");
} else {
websocket = socket("http://IPADDRESS:2999");
}
this.state = {
game: null,
socket: websocket,
right_gui_open: false,
player: {name:"Undefined", x:0, y:0},
player_request: [],
}
let getGame = (gameFiles) => {
this.setState({game: JSON.parse(gameFiles)});
this.state.socket.emit("getGame", this.props.state.server_id, this.props.state.username, this.state.player_request);
}
this.state.socket.emit("getGame", this.props.state.server_id, this.props.state.username, this.state.player_request);
this.state.socket.on("gameFiles", getGame);
this.closeRightGui = this.closeRightGui.bind(this);
this.openRightGui = this.openRightGui.bind(this);
}
closeRightGui(){
this.setState({right_gui_open: false});
}
/* Right Here is where it errors
/////
/////
/////
////
/////*/
openRightGui(){
console.log(this.state); // correctly outputs state
this.setState({right_gui_open: false}); // this line breaks all other calls
}
render(){
if (this.state.game === null){
return (<div>Not connected to Game yet</div>);
} else {
this.setState({player:this.state.game.players.filter(player => player.name === this.props.state.username)[0]});
let player_gui_display = this.state.game.players.map((player, i) => {
if (player.name !== this.state.player.name){
return (
<div class='player-gui-display' id={i}><div>{player.name}</div><div class='coin-shower'>Coins: {player.coins}</div></div>)
} else {
return '';
}
})
let right_gui = (
<div>
<button class='lift' onClick={this.closeRightGui}>Close</button>
<div class='server-show'>Server: {this.state.game.server_name}</div>
<div class='player-gui'>
Players
{player_gui_display}
</div>
</div>)
return (
<div class='container'>
<div class='game'>
<div class='game-stuff' style={{"top": centerY(this.state.player.y), "left": centerX(this.state.player.x)}}>
<img src={game_grid} alt='Game Background'/>
{this.state.game.enemies.map(enemy => getEnemy(enemy))}
</div>
<div class='gui'>
<div class='top-gui'>
{this.state.player.x}
</div>
<div class='right-gui'>
{this.state.right_gui_open ? right_gui : (<button class='lift' onClick={this.openRightGui}>Open</button>)}
</div>
</div>
</div>
</div>
)
}
}
}
export default Game;
I appreciate any help people can give me.