Results 1 to 7 of 7

Thread: creating a jump

  1. #1
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default creating a jump

    I'm just working on making a simple ball game right now and I want the ball to go up 100 pixels then come down when you hit space bar. But for some reason I either get the ball to go up and not come down or it just freezes. Here the space bar part of my code where it just keeps floating up:

    else if (key == 32)
    {
    if (y_pos > 150)
    {
    y_speed = -1;
    }
    else
    {
    y_speed = 1;
    }
    }


    yes there is another if above the else if so thats not it.

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

    Default

    I want the ball to go up 100 pixels then come down
    Where do you paint the ball?
    How does that code determine where to paint the ball?

    What does this mean: key == 32
    Is it the same as: key == ' '

    Have you tried debugging the code by adding println()s to show how the values for the y location of the ball changes?
    Last edited by Norm; 07-21-2010 at 05:01 PM.

  3. #3
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    key == 32 is the detection of the space bar being pressed. I draw a circle like this:

    public void paint (Graphics g)
    {
    g.setColor (Color.red);

    g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
    }

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

    Default

    Have you tried debugging the code by adding println()s to show how the values for the y location of the ball changes?
    Last edited by Norm; 07-21-2010 at 05:01 PM.

  5. #5
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    O yeah and x and y pos are determined like this:

    x_pos += x_speed;
    y_pos += y_speed;

  6. #6
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Umm I just compile using the javac code and then I put it in a web page coding.

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

    Default

    Have you tried debugging the code by adding println()s to show how the values for the y location of the ball changes?

Similar Threads

  1. Creating jar
    By nitinverma in forum AWT / Swing
    Replies: 1
    Last Post: 07-12-2010, 11:07 AM
  2. Creating files stopped creating...
    By Dieter in forum Advanced Java
    Replies: 3
    Last Post: 09-25-2009, 11:45 PM
  3. Creating .Jar files
    By Deathmonger in forum New To Java
    Replies: 6
    Last Post: 06-22-2008, 11:59 PM
  4. Eclipse- jump to method definition
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-07-2007, 03:34 PM
  5. To truncate jump of line in jsp
    By Eric in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-09-2007, 03:58 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
  •