SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If statement(s) not working correctly

    I'm having some problems with the collision detection for this breakout game of mine. The stuff I'm looking at is in the checkBlocks function. The ball moves very erratically. As you can see I've tried quite a few things. First I just put the positions of the blocks in some variables inside the for loop, then I tried putting them in an array at the top of the code (before the startGame function). Both give the same results. The if statements (commented and uncommented) all look fine to me. Does anyone know what's wrong?

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still need help...

    I did discover a small thing though. In the loops to check every block, it doesn't seem to work if the max number of loops is an even number. If it's an odd number, like 39, more stuff works. Although it doesn't check the last block obviously. I didn't test this stuff with all of those if statements, I just made a simple one that checked if the top of the ball reached the top of the blocks. Although when it detected the collision, it detected it for the whole bottom row instead of a single block. Any idea why this happens?

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I made some progress...I got the ball to bounce correctly, in a way (link updated in my first post). I have 2 if statements that are basically the same thing. They work the way they should when they're alone (meaning when the other one is commented out). But when they're together, they both evaluate to true, meaning the ball goes back in the opposite direction it came from. Also, if I change the < and > in the first if statement to <= and >=, that one bounces in the right direction but it still bounces incorrectly off the sides. I can't seem to do a weird combination like that for the opposite to happen.

    Please help guys, I can't figure this out.

    PS: the erratic behavior was rather simple to fix. Instead of evaluating ballTop < (blocksTop[count] + 20) I did (ballTop - 20) < blocksTop[count].


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •