Results 1 to 3 of 3
- 07-11-2012, 11:04 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Intersects() works just first time
Hello every1! :)
I'm coding a Ping Pong game. It's almost over, but if player lost round his paddle is doing strange things... If I win there's everything alright.
Please, watch video: stackoverflow - Dailymotion Wideo You see left paddle after MsgBox (0:18+)? I don't know what is causing that behavior. IMO the code is fine.
Method responsible for intersection:
If you need more code, just write. :)Java Code:Runnable r1 = new Runnable() { public void run() { try { while (true) { board.pPaddle.setBounds(board.p.getX(), board.p.getY(), 10, 50); board.aiPaddle.setBounds(board.a.getX(), board.a.getY(), 10, 50); board.ball.setBounds(board.b.getX(), board.b.getY(), 10, 10); if (getY() < 1) { //ceiling y = 2; speedY = 1.11 + local.nextDouble() + 0.11; } else if (getY() > 261) { //floor speedY = -speedY; } if (board.ball.intersects(board.pPaddle)) { //ball intersects player's paddle direct = local.nextInt(3); if (direct > 1) { speedY = local.nextInt(3) + 0.11 + local.nextDouble(); } else { speedY = -local.nextInt(3) + local.nextDouble(); } speedY = local.nextInt(3) + 0.11; speed = -speed; } else if (board.ball.intersects(board.aiPaddle)) { //ball intersects computer's paddle direct = local.nextInt(3); if (direct > 1) { speedY = local.nextInt(3) + 0.11 + local.nextDouble(); } else { speedY = -local.nextInt(3) + local.nextDouble(); } speed = -speed; speedY = -speedY; } x += speed; y += speedY; board.repaint(); Thread.sleep(10L); board.checkWin(); } }catch(InterruptedException e){ } } }; }
Regards,
AdrianLast edited by Szinek; 07-11-2012 at 11:06 PM.
-
Re: Intersects() works just first time
Whenever I see this, I know for a fact that the code is not fine. Trust me, you've got a bug in your code, not in Java itself. The problem is finding the bug. Consider using a debugger or println statements or a logger to see what the state of your variables are at various points in your program.
I do see things that worry me, mainly that you're making GUI calls off of the event thread, and this can cause strange bugs. But otherwise your first order of business is to identify the error, and that's going to take effort you your part. Find out what changes in the program's state if the player loses as that would be a good place to start.
- 07-12-2012, 10:44 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Re: Intersects() works just first time
Now I know what's happening. After first lost round speeds are going mad, just for the first collision.
I'm trying to figure it out, thanks, Fubarable.java.awt.Rectangle[x=4,y=190,width=10,height=50] X4 Y190 speedY 0.11 speedX 1.5
java.awt.Rectangle[x=4,y=190,width=10,height=50] X4 Y190 speedY -0.89 speedX -1.5
java.awt.Rectangle[x=4,y=190,width=10,height=50] X4 Y190 speedY -1.89 speedX 1.5
java.awt.Rectangle[x=4,y=190,width=10,height=50] X4 Y190 speedY -0.89 speedX -1.5
No idea :/Last edited by Szinek; 07-12-2012 at 11:36 AM.
Similar Threads
-
enter the time zone offset to GMT and convert to current time
By Bonia in forum New To JavaReplies: 5Last Post: 02-29-2012, 10:07 AM -
Desingn time properties missing at run time - Netbeans
By junaid in forum New To JavaReplies: 3Last Post: 12-07-2011, 07:00 AM -
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 12:46 PM -
Java GlassPane Works 30% Of The Time.
By Alerhau in forum AWT / SwingReplies: 15Last Post: 08-10-2011, 05:41 PM -
help with intersects() and contains()
By Grid_iso in forum New To JavaReplies: 8Last Post: 05-28-2011, 12:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks