我想帮助修复我的代码

时间:2017-02-03 18:08:16

标签: javascript

   /* Constants */
var START_RADIUS = 1;
var INCREMENT = 1;
var CHANGE_COLORS_AT = 10;
var circle;

function start(){
    //Circle is being added once in the start function.
    circle = new Circle(START_RADIUS);
    circle.setPosition(getWidth()/2, getHeight()/2);
    add(circle);

    //This is the command that will execute every 50 miliseconds.
    setTimer(grow, 50);
}

function grow(){
    //This will keep the circle from continually growing past the height of the (premade) canvas.
    while(circle.getRadius()*2 != getHeight()){
        START_RADIUS = START_RADIUS + INCREMENT;
        circle.setRadius(START_RADIUS);
        //Changes the color every +10 the radius grows.
        if(circle.getRadius() % CHANGE_COLORS_AT == 0){
            circle.setColor(Randomizer.nextColor());
        }
    }
}

此代码旨在制作一个不断增长的圆圈(直到直径达到画布顶部)。这是针对学校的,并且正在使用来自网站' codehs.com'的非常简化的javascript版本。我一直在研究这段代码,并想了解如何解决它。

1 个答案:

答案 0 :(得分:2)

实际修好了。问题是,有一个"而#34;循环," setTimer"命令,也或多或少充当while循环。这使圆圈立即膨胀到全尺寸。固定代码在这里!VV

/* Constants */
var START_RADIUS = 1;
var INCREMENT = 1;
var CHANGE_COLORS_AT = 10;
var circle;

function start(){
    //Circle is being added once in the start function.
    circle = new Circle(START_RADIUS);
    circle.setPosition(getWidth()/2, getHeight()/2);
    add(circle);

    //This is the command that will execute every 50 miliseconds.
    setTimer(grow, 5);
}

function grow(){
    START_RADIUS = START_RADIUS + INCREMENT;
    circle.setRadius(START_RADIUS);
    if(circle.getRadius() % CHANGE_COLORS_AT == 0){
        circle.setColor(Randomizer.nextColor());
    }
}