Hunter
Hunter

Reputation: 1

I can't get all the circles to change color in the codehs javascript changing circles program

var NUM_CIRCLES = 15;
var MIN_RADIUS = 10;
var MAX_RADIUS = 40;
var DELAY = 500;
function start(){
    drawCircles();
    setTimer(update, DELAY);
}

function drawCircles(){
    for(var i = 0; i < NUM_CIRCLES; i++){
        circle = new Circle(Randomizer.nextInt(MIN_RADIUS, MAX_RADIUS));
        var x = Randomizer.nextInt(MIN_RADIUS, getWidth() - MAX_RADIUS);
        var y = Randomizer.nextInt(MIN_RADIUS, getHeight() - MAX_RADIUS);
        circle.setPosition(x, y);
        add(circle);
    }
}

function update(){
    var colors = Randomizer.nextColor();
    circle.setColor(colors);
}

Write a program that creates a list of NUM_CIRCLES circles on the screen of different sizes from MIN_RADIUS to MAX_RADIUS. Every DELAY milliseconds, get a random color and change all of the circles to this color. All the circles should have the same color at all times.

Upvotes: 0

Views: 12161

Answers (2)

eubydied
eubydied

Reputation: 11

First, you should declare circle as a global variable since you use circle in multiple functions. Next you should get rid of the drawCircles function in the start function and put it inside the timer. Then call the update function in your drawCircles function.

This is the revised code.

var NUM_CIRCLES = 15;
var MIN_RADIUS = 10;
var MAX_RADIUS = 40;
var DELAY = 500;
var circle;
function start(){
    setTimer(drawCircles, DELAY);
}

function drawCircles(){
    for(var i = 0; i < NUM_CIRCLES; i++){
        circle = new Circle(Randomizer.nextInt(MIN_RADIUS, MAX_RADIUS));
        var x = Randomizer.nextInt(MIN_RADIUS, getWidth() - MAX_RADIUS);
        var y = Randomizer.nextInt(MIN_RADIUS, getHeight() - MAX_RADIUS);
        update();
        circle.setPosition(x, y);
        add(circle);
    }
}

function update(){
    var colors = Randomizer.nextColor();
    circle.setColor(colors);
}

Upvotes: 1

Tyler Hudson
Tyler Hudson

Reputation: 30

 setTimer(update, DELAY);

here when you call your update function you do not have () after update change it to:

 setTimer(update(), DELAY);

Upvotes: 0

Related Questions