Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8401409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:47:56+00:00 2026-06-09T21:47:56+00:00

i have a logic problem that maybe you smart guys can help me to

  • 0

i have a logic problem that maybe you smart guys can help me to resolve.
I am learning Javascript with Canvas and i’m trying to build a sort of “brakeout” game using pure Javascript.

http://www.seas.upenn.edu/~cis120e/hw/SwingGame/images/breakout.png

I created the paddle, the ball, and then an array of blocks-Objects with different properties(xPos, yPos, width…etc), now i am trying to detect the collision of the ball with the blocks on the top of the screen.

This is the way i do:

//This is my block Object definition
function block(x,y,width,height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
ctxBlock.fillStyle = 'green';
ctxBlock.fillRect(this.x, this.y, this.width, this.height);

}

//This is for draw blocks on the canvas
mattone.prototype.draw = function(){
ctxBlock.fillStyle = 'green';
ctxBlock.fillRect(this.x, this.y, this.width, this.height);

}

//This is the check for collision method
var blocks = [];
function checkCollision(){
for (var i = 0; i < blocks.length; i++){

    if(ballY - ballRadius <= blocks[i].y + blockWidth){
        ctxBlocks.clearRect(tmpx, tmpy, blockWidth, blockHeight);
        speedY = (-speedY);
        break;
    }
}

checkColl = requestAnimationFrame(checkCollision);

}

Basically i am trying to tell the canvas that when the “Y” of the ball is “<” then the “Y” of one of the block in the array of blocks(looping trough and checking for every single block), he must delete that block and bounce down.

But what it does is always cancel the first block on the left in that line of blocks, and not the block he touch.

This should be the link to my code link
*Plese note i used the word mattoni instead of block in my code

Have a nice day to you all

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:47:58+00:00Added an answer on June 9, 2026 at 9:47 pm

    Very nice! Thank you for providing a complete jsfiddle, too. I’ve made an update to that:
    http://jsfiddle.net/43mwc/2/ . Now for an explanation: your collision check simply needs to check all bounds of each block, not just the bottom as you’re doing. So I changed this:

    if(ballY - ballRadius <= blocks[i].y + blockWidth){
    

    to this (with your jsfiddle’s wording):

    if(ballY-ballRadius <= mattoni[i].y + 10- padding && (ballX - ballRadius) >= mattoni[i].x && (ballX + ballRadius) <= (mattoni[i].x + mattWidth)){
    

    To explain, your code looped through every brick checking if the ball was directly below each brick. The whole bottom row of bricks satisfies that criterion, so your loop broke (as in break;) on the first brick in the loop which lay on the bottom row. Now the ball’s position along the x axis is being checked to be between each brick’s left and right edges. There’s still one scenario that’s not covered here: bricks being hit from the top. I’ll leave that one to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little dilemma that maybe you can help me sort out. I've
So here is the problem: I have some logic in my application that will
The problem I am encountering is that for my login form I have to
I have a problem with the logic of this code, XNA's Update Method is
I have authentication logic that uses cookies. The model attributes are initialized from cookies,
I'm having a hard time describing this problem. Maybe that's why I'm having a
I have a problem with django's MVC. I understand that this is not the
I hope you guys have time for an old dog (71 years old) learning
I have the same problem as described in the posts listed below. That is,
I’m having a little Architecture problem. In my project I have a Business Logic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.