Estoy haciendo un juego de aves de vista superior e intento cambiar el color de solo uno de los elementos elegidos de la matriz. Soy bastante nuevo en JavaScript y en general. Creo que hice la parte de elección correcta, pero haga lo que haga, no pude cambiar el color. Solo tengo que cambiar la parte del color, por lo que no incluí el resto del código. ¡Salud!
//Drawing the basic bird shape function drawBird(ctx, bird) { const angle = Math.atan2(bird.dy, bird.dx); ctx.translate(bird.x, bird.y); ctx.rotate(angle); ctx.translate(-bird.x, -bird.y); ctx.fillStyle = "yellow"; ctx.beginPath(); ctx.moveTo(bird.x, bird.y); ctx.lineTo(bird.x - 30, bird.y + 10); ctx.lineTo(bird.x - 30, bird.y - 10); ctx.lineTo(bird.x, bird.y); ctx.fill(); ctx.setTransform(1, 0, 0, 1, 0, 0);} //Choosing one of the elements of the array const randomElement = birds[Math.floor(Math.random() * birds.length)]; //Here suppose to be changing the colorPuede agregar un parámetro de color a su función drawBird y ejecutarlo nuevamente después de elegir un pájaro al azar
//Drawing the basic bird shape function drawBird(ctx, bird, color) { const angle = Math.atan2(bird.dy, bird.dx); ctx.translate(bird.x, bird.y); ctx.rotate(angle); ctx.translate(-bird.x, -bird.y); ctx.fillStyle = color; ctx.beginPath(); ctx.moveTo(bird.x, bird.y); ctx.lineTo(bird.x - 30, bird.y + 10); ctx.lineTo(bird.x - 30, bird.y - 10); ctx.lineTo(bird.x, bird.y); ctx.fill(); ctx.setTransform(1, 0, 0, 1, 0, 0); } //Choosing one of the elements of the array const randomElement = birds[Math.floor(Math.random() * birds.length)]; //Here suppose to be changing the color drawBird(ctx, randomElement, "purple");