Page 3 of 3 FirstFirst 123
Results 41 to 59 of 59

Thread: 2D Game

  1. #41
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    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.

  2. #42
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    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
    This is the output before the x and y values are changed depending on the velocity. As you can see it seems to move correctly.. So should I make this output at multiple different places as well?

  3. #43
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    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.

  4. #44
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    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.

  5. #45
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    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).

  6. #46
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    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.

  7. #47
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    How do I do something like that if it uses:

    Java Code:
    			x += Math.round( velocityX );
    			y += Math.round( velocityY );
    to update the positions of the sprite and:

    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" );
    				
    			}
    to set the velocity? It isn't exactly one pixel per second, it's more one pixel for every loop in run( ).

  8. #48
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    How long does it take to do a loop in run()?

  9. #49
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    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 );
    		
    	}
    The timePassed variable I believe.

  10. #50
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    thread.sleep( sleepTime / 1000000L );

    What does the above do? Its integer arithmetic.
    What are the values of sleepTime?

  11. #51
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    It tells you in the code

    Java Code:
    sleepTime = ( period - timeDiff ) - overSleepTime;

  12. #52
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    How can I tell what the value of sleepTime is from your equation.
    Is the value 4 or 4567833???

  13. #53
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    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

  14. #54
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    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?

  15. #55
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    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
    Around this in milliseconds.

  16. #56
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    I've lost the problem.
    You said:
    move slow as hell because you only move one pixel per second (or so)
    How fast is it moving now?
    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.

  17. #57
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    The movement isn't really slow, but it's still slow.

    Java Code:
    			x += Math.round( velocityX );
    			y += Math.round( velocityY );
    The sprite moves 1 pixel per loop, as the velocity is 1. If I increase the velocity however, it skips pixels out of the collision checker.

  18. #58
    Elffus is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    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!

  19. #59
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  3. Help! Game.
    By MIA6 in forum New To Java
    Replies: 4
    Last Post: 11-08-2009, 12:22 AM
  4. Game 21
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 12:33 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM

Posting Permissions

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