Results 41 to 59 of 59
Thread: 2D Game
 08042010, 06:39 PM #41
Don't need a live demo. I'm trying to give you ideas on how to debug your program.
You need to output variables as they are being changed to show you what is happening. By looking at the output you should see a place where the variables are not what you expect them to be. Then you need to find out where in your logic that is happening.
 08042010, 08:28 PM #42Member
 Join Date
 Aug 2010
 Posts
 32
 Rep Power
 0
Java Code:x: 25.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 28.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 36.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 46.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 55.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 66.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 79.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 89.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 98.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 106.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 116.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 127.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 136.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 152.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 167.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 186.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 197.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 206.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 216.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 224.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 233.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 242.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 252.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 261.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 274.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 286.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 295.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 305.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 313.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 320.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 329.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 337.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 346.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 355.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 364.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 380.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 389.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 400.0  velocityX: 0.1  y: 351.0  velocityY: 0.0 x: 409.0  velocityX: 0.0  y: 351.0  velocityY: 0.0
 08042010, 08:47 PM #43
Did you find a case where it acted incorrectly? It was going thru a barrier when it should have stopped. You need to run the debug output to see how/what the variables are when that happens.
 08042010, 09:36 PM #44Member
 Join Date
 Aug 2010
 Posts
 32
 Rep Power
 0
So I debugged the collision function to return every colour it checks. I figure it just isn't checking every pixel and is skipping some  because it doesn't pick it up.
I tried moving the collision code to the other side, so it checks after the values are set, but that doesn't work either. I really have no idea.
 08042010, 09:54 PM #45Member
 Join Date
 Aug 2010
 Posts
 32
 Rep Power
 0
Heh, I set the velocity to 1 and made it just increment by the velocity instead of velocity * timePassed, and it works perfectly. Only problem is you move slow as hell because you only move one pixel per second (or so).
 08042010, 10:04 PM #46
Well good, that's progress.
Now change it from one pixel per second to one pixel per 1/nth of a second, where n is some value you find by experimenting with the code to see what seems right.
 08042010, 10:10 PM #47Member
 Join Date
 Aug 2010
 Posts
 32
 Rep Power
 0
How do I do something like that if it uses:
Java Code:x += Math.round( velocityX ); y += Math.round( velocityY );
Java Code:public void keyPressed( KeyEvent e ) { int code = e.getKeyCode( ); if ( code == KeyEvent.VK_RIGHT && playerPressRight == false ) { player.setVelocityX( 1.0f ); playerPressRight = true; playerPressLeft = false; loadImages( 3 ); //System.out.println( "KEYPRESS RIGHT" ); }
 08042010, 10:22 PM #48
How long does it take to do a loop in run()?
 08042010, 10:27 PM #49Member
 Join Date
 Aug 2010
 Posts
 32
 Rep Power
 0
Java Code:public void run( ) { //System.out.println( "Inside run" ); long beforeTime, afterTime, timeDiff, sleepTime, timePassed, cumulativeTime; long period = 10; long overSleepTime = 0L; int noDelays = 0; cumulativeTime = System.currentTimeMillis( ); isRunning = true; while ( isRunning ) { beforeTime = System.currentTimeMillis( ); timePassed = System.currentTimeMillis( )  cumulativeTime; cumulativeTime += timePassed; gameUpdate( timePassed ); gameRender( ); gameDraw( ); afterTime = System.currentTimeMillis( ); timeDiff = afterTime  beforeTime; sleepTime = ( period  timeDiff )  overSleepTime; if ( sleepTime > 0 ) { try { thread.sleep( sleepTime / 1000000L ); } catch ( Exception ex ) { System.out.println( "Error  Run  Core.java : " + ex ); } overSleepTime = ( System.currentTimeMillis( )  afterTime )  sleepTime; } else { overSleepTime = 0L; if ( ++noDelays >= NO_DELAYS_PER_YIELD ) { Thread.yield( ); noDelays = 0; } } } System.exit( 0 ); }
 08042010, 10:34 PM #50
thread.sleep( sleepTime / 1000000L );
What does the above do? Its integer arithmetic.
What are the values of sleepTime?
 08042010, 10:45 PM #51Member
 Join Date
 Aug 2010
 Posts
 32
 Rep Power
 0
It tells you in the code
Java Code:sleepTime = ( period  timeDiff )  overSleepTime;
 08042010, 11:13 PM #52
How can I tell what the value of sleepTime is from your equation.
Is the value 4 or 4567833???
 08052010, 12:04 AM #53Member
 Join Date
 Aug 2010
 Posts
 32
 Rep Power
 0
It has no defined value, it depends on how long it takes to complete Render, Draw and Update, here's a console print of the values:
Java Code:SLEEEEP TIME: 5 SLEEEEP TIME: 34 SLEEEEP TIME: 11 SLEEEEP TIME: 6 SLEEEEP TIME: 12 SLEEEEP TIME: 19 SLEEEEP TIME: 24 SLEEEEP TIME: 11 SLEEEEP TIME: 4 SLEEEEP TIME: 43 SLEEEEP TIME: 22 SLEEEEP TIME: 17 SLEEEEP TIME: 84 SLEEEEP TIME: 82 SLEEEEP TIME: 29 SLEEEEP TIME: 14 SLEEEEP TIME: 40 SLEEEEP TIME: 2 SLEEEEP TIME: 9 SLEEEEP TIME: 16 SLEEEEP TIME: 24 SLEEEEP TIME: 26 SLEEEEP TIME: 33 SLEEEEP TIME: 40 SLEEEEP TIME: 47 SLEEEEP TIME: 52 SLEEEEP TIME: 57 SLEEEEP TIME: 65 SLEEEEP TIME: 69 SLEEEEP TIME: 74 SLEEEEP TIME: 76 SLEEEEP TIME: 84 SLEEEEP TIME: 86 SLEEEEP TIME: 90 SLEEEEP TIME: 93 SLEEEEP TIME: 99 SLEEEEP TIME: 104 SLEEEEP TIME: 107 SLEEEEP TIME: 111 SLEEEEP TIME: 114 SLEEEEP TIME: 120 SLEEEEP TIME: 119 SLEEEEP TIME: 119 SLEEEEP TIME: 112 SLEEEEP TIME: 82 SLEEEEP TIME: 80 SLEEEEP TIME: 68 SLEEEEP TIME: 66 SLEEEEP TIME: 64 SLEEEEP TIME: 63 SLEEEEP TIME: 56 SLEEEEP TIME: 40 SLEEEEP TIME: 23 SLEEEEP TIME: 21 SLEEEEP TIME: 19 SLEEEEP TIME: 15 SLEEEEP TIME: 20 SLEEEEP TIME: 2 SLEEEEP TIME: 0 SLEEEEP TIME: 4 SLEEEEP TIME: 4 SLEEEEP TIME: 11 SLEEEEP TIME: 18 SLEEEEP TIME: 24 SLEEEEP TIME: 28 SLEEEEP TIME: 32 SLEEEEP TIME: 39 SLEEEEP TIME: 45 SLEEEEP TIME: 48 SLEEEEP TIME: 56 SLEEEEP TIME: 60 SLEEEEP TIME: 68 SLEEEEP TIME: 75 SLEEEEP TIME: 82 SLEEEEP TIME: 39
 08052010, 12:12 AM #54
Did you say that the loop takes 1 second per loop?
How long do these method calls take:
gameUpdate( timePassed );
gameRender( );
gameDraw( );
Is the objective is to update the position faster than one time per second?
 08052010, 12:22 AM #55Member
 Join Date
 Aug 2010
 Posts
 32
 Rep Power
 0
No. It takes however long it takes to loop to loop. Hang on...
Java Code:TIME DIFFF: 34 TIME DIFFF: 28 TIME DIFFF: 22 TIME DIFFF: 21 TIME DIFFF: 23 TIME DIFFF: 24 TIME DIFFF: 22 TIME DIFFF: 23 TIME DIFFF: 22 TIME DIFFF: 22 TIME DIFFF: 25 TIME DIFFF: 23 TIME DIFFF: 22 TIME DIFFF: 22 TIME DIFFF: 22 TIME DIFFF: 26 TIME DIFFF: 49
 08052010, 12:29 AM #56
I've lost the problem.
You said:move slow as hell because you only move one pixel per second (or so)
The loop times you show are much faster the 1000 ms. So why are the movements 1 pixel per sec. Are there many times when the moves are 0 pixels? If in 200 loops, it only moves once then that could account for the slow movement.
 08052010, 12:37 AM #57Member
 Join Date
 Aug 2010
 Posts
 32
 Rep Power
 0
The movement isn't really slow, but it's still slow.
Java Code:x += Math.round( velocityX ); y += Math.round( velocityY );
 08052010, 01:13 AM #58Member
 Join Date
 Aug 2010
 Posts
 32
 Rep Power
 0
Doesn't matter, I got it working by simply grabbing a rectangle of pixels as far as the velocity, so it'd grab all the pixels in a rectangle in front of the player instead of just one row in front. Thanks for all your help, it has most certainly helped!
 08052010, 01:47 AM #59
Similar Threads

Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08052010, 04:49 AM 
game code for any game
By deathnote202 in forum Java GamingReplies: 4Last Post: 06102010, 08:06 AM 
Help! Game.
By MIA6 in forum New To JavaReplies: 4Last Post: 11082009, 01:22 AM 
Game 21
By aRTx in forum Advanced JavaReplies: 3Last Post: 04042009, 12:33 AM 
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02102009, 07:00 AM
Bookmarks