Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
Chancing the color of a random chosen object from an array

I am making a top view bird game, and trying to change the color of only one of the chosen element from the array. I am pretty new to JavaScript and general. I think I did the choosing part correct, but whatever I do, couldn't manage to change the color. I am only stuck with changing the color part, so didn't include the rest of the code. Cheers!

//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 color
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can add a color parameter to your drawBird function and run it again after randomly choosing a bird

//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");
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda