Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: gravity in 2d game

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default gravity in 2d game

    when user hit up key, jump get set to true and when user let go of up key jump get set to false.
    gravity = 2
    velocity = 20
    y = y postion of my player


    problem:

    1st - if i hold up key my player go up 20 pics, but when i keep on going down for ever.
    2nd - if i hit up key and let go, then hit up key and let go, and so on ..... it keep on going up for ever.

    while(true){
        if(jump == true){
          velocity -= gravity;
           y -= velocity;	
        }
        else if(jump == false){
          velocity = 20;
        }
    }


    problem #2 is bc i am seting velocity = 20 in else if. to fix this problem i can just del the statment.
    problem #1 is really hard and i cant find a good sloution for it. may be if i have a test inside of
     if(jump == true){


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: gravity in 2d game

    And what happened when you tried that?

    By the way, putting your game logic inside an infinite loop like that probably isn't a good idea. It's better to use a Timer.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Need help with tic tac toe game
    By ogpg2006 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 07:34 AM
  2. Game Maker Language and Game Maker and Zelda Classic thread
    By Fira in forum Other Programming Languages
    Replies: 3
    Last Post: April 17th, 2012, 08:59 AM
  3. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  4. Need Help with Game
    By Shivam24 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 18th, 2011, 03:58 PM
  5. Game !
    By Ahmed.Ibrahem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 08:01 PM