Question: I’m making an io game and i’m trying to draw players to the screen. The problem is that ctx.clearRect()
isn’t working… I’ve tried putting it after ctx.fill()
but then nothing shows up on the screen. How do I fix this?
Repl link: https://replit.com/@ColoredHue/BulletPartyio
function spawnGame() {
move = true
setTimeout(function() {
//Draw a player rectangle at x,y
function drawPlayer(player, playerId) {
console.log(player, playerId)
if (Object.keys(gamestate[LobbyId])[0] == playerId) {
ctx.fillStyle = "red";
} else if (Object.keys(gamestate[LobbyId])[1] == playerId) {
ctx.fillStyle = "yellow";
} else if (Object.keys(gamestate[LobbyId])[1] == playerId) {
ctx.fillStyle = "green";
} else if (Object.keys(gamestate[LobbyId])[3] == playerId) {
ctx.fillStyle = "blue";
}
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.rect(player.x, player.y, 25, 25);
ctx.fill();
//draw the players that the server sent
}
for (let player in gamestate[LobbyId]) {
drawPlayer(gamestate[LobbyId][player], player)
}
spawnGame()
}, 1000/60);
}```