0
function blocksLogic(){

if(gameRunning == 0){
    var blocks = new Array(7);
    for(var i=0; i <7; i++){
        blocks[i] = new Array(7);
    }
    for(var x=0; x < 7; x++){
        for(var y=0; y < 7; y++){
            blocks[x][y] = false;
            console.log(blocks[x][y]);
        }
    }
}
console.log("gamerunning function ran");
// COLLISION!!!!!!!
for(var brickX = 0; x < 7; x++){
    console.log("for x has been run!");
    for( var brickY = 0; y < 7; y++){
            console.log("for y has been run!");
            var tempBrickX = brickX * 105 + 34;
            var tempBrickY = brickY * 25 - 10;
            //top collision
        if(ballY >= tempBrickX && ballX >= tempBrickX && ballX <= tempBrickX + BRICK_WIDTH){
            console.log("The top of this block has been hit!");
            ballSpeedX = -ballSpeedX;
            ballSpeedY = -ballSpeedY;
        }
            //bottom collision
        if(ballY <= tempBrickY + BRICK_HEIGHT && ballX >= tempBrickX  && ballX >= tempBrickX){
            console.log("The bottom of this brick has been hit!");
            ballSpeedX = -ballSpeedX;
            ballSpeedY = -ballSpeedY;
        }       
    }
}

https://pastebin.com/t2Zq79BG

The function blocksLogic is not running the code below the comment "//COLLISION!" its probably something really simple but im just starting to get into coding with javascript(thats why my code formatting sucks) i added alot of debugging console.logs to see what was running and what wasnt.

2 Answers 2

2

for(var brickX = 0; x < 7; x++)

x is undefined here, that's why the loop never runs (this looks like a copy paste error from above). Make sure to use brickX instead:

for(var brickX = 0; brickX < 7; brickX++)

Sign up to request clarification or add additional context in comments.

Comments

1

Check your variables inside for loops. Why do you declare brickX, but then check x? Change to

for(var x = 0; x < 7; x++){
console.log("for x has been run!");
for( var y = 0; y < 7; y++){
        console.log("for y has been run!");
        var tempBrickX = x * 105 + 34;
        var tempBrickY = y * 25 - 10;
        //top collision
    if(ballY >= tempBrickX && ballX >= tempBrickX && ballX <= tempBrickX + BRICK_WIDTH){
        console.log("The top of this block has been hit!");
        ballSpeedX = -ballSpeedX;
        ballSpeedY = -ballSpeedY;
    }
        //bottom collision
    if(ballY <= tempBrickY + BRICK_HEIGHT && ballX >= tempBrickX  && ballX >= tempBrickX){
        console.log("The bottom of this brick has been hit!");
        ballSpeedX = -ballSpeedX;
        ballSpeedY = -ballSpeedY;
    }       
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.