无限" for"环

时间:2017-02-17 16:46:50

标签: javascript

我正在为Javascript编写项目突破游戏的代码,现在我正在添加砖块。不知何故,我创造了一个无限循环。我知道这是正确的代码,至少把砖块放在屏幕上,但代码只是不能在我的电脑上运行,它只是打破了。有人可以确定无限循环的创建位置吗?谢谢!

//Brick Constants
var NUM_ROWS = 8;
var BRICK_TOP_OFFSET = 10;
var BRICK_SPACING = 2;
var NUM_BRICKS_PER_ROW = 10;
var BRICK_HEIGHT = 10;
var SPACE_FOR_BRICKS = getWidth() - (NUM_BRICKS_PER_ROW + 1) * BRICK_SPACING;
var BRICK_WIDTH = SPACE_FOR_BRICKS / NUM_BRICKS_PER_ROW;

//Paddle Constants
var PADDLE_WIDTH = 80;
var PADDLE_HEIGHT = 15;
var PADDLE_OFFSET = 10;

//Ball Constants
var BALL_RADIUS = 15;

var brick;

function start(){
    addBricks();
}

function addBricks(){
    var brickSepY = BRICK_TOP_OFFSET;
    for(var i = 0 ; i < NUM_ROWS ; i++){
        var brickSepX = BRICK_SPACING;
        for(var j = 0 ; j < NUM_BRICKS_PER_ROW ; i++){
            brick = new Rectangle(BRICK_WIDTH, BRICK_HEIGHT);
            brick.setPosition(brickSepX, brickSepY);
            add(brick);
        }
    }
}

2 个答案:

答案 0 :(得分:6)

变化:

for(var j = 0 ; j < NUM_BRICKS_PER_ROW ; i++)
//                                  here ^^^

到:

for(var j = 0 ; j < NUM_BRICKS_PER_ROW ; j++)
//                                  here ^^^

答案 1 :(得分:0)

您正在增加第一个 j

function addBricks(){
    var brickSepY = BRICK_TOP_OFFSET;
    for(var i = 0 ; i < NUM_ROWS ; i++){
        var brickSepX = BRICK_SPACING;
        for(var j = 0 ; j < NUM_BRICKS_PER_ROW ; **i++**){
            brick = new Rectangle(BRICK_WIDTH, BRICK_HEIGHT);
            brick.setPosition(brickSepX, brickSepY);
            add(brick);
        }
    }
}