Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!