0

So I'm trying to get this rectangle to perform a jumping motion every time I hit the space bar. When I tap the space bar the motion is fine. It goes up and comes back down, but the motion isn't very fluid. It just immediately comes up to the high point and then immediately goes back to the ground. How do I fix this? Here is the code for the jumping motion (Jump is what is called upon pressing the space key and Fall is on the release of the space key) :

class Jump extends AbstractAction 
        {
            public void actionPerformed(ActionEvent e)
            {
                while(time<2.5) 
                {
                    time+=0.1;
                    py-=5-(2*(time));
                    if(py>=300)
                    {
                        py=300;
                        py-=0;
                    }
                    repaint();
                    System.out.println(time);
                }   

            }
        }
        class Fall extends AbstractAction
        {
            public void actionPerformed(ActionEvent e)
            {
                while(time>0)
                {
                    time-=0.1;
                    py+=5-(2*(time));
                    if(py>=300)
                    {
                        py=300;
                        py-=0;
                    }
                    repaint();
                }

            }
        }
user1058860
  • 513
  • 2
  • 9
  • 21
  • What is the purpose of `py -= 0;` statement? – PM 77-1 Aug 25 '14 at 02:11
  • 2
    From the looks of it, you are blocking the Event Dispatching Thread. Take a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [How to use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for more details – MadProgrammer Aug 25 '14 at 02:11
  • 1
    And [for example](http://stackoverflow.com/questions/13022754/java-bouncing-ball/13022788#13022788) and [example](http://stackoverflow.com/questions/14593678/multiple-bouncing-balls-thread-issue/14593761#14593761) and [example](http://stackoverflow.com/questions/21198074/why-my-code-bouncing-ball-doesnt-work/21198101#21198101) and ... oh you get the idea... – MadProgrammer Aug 25 '14 at 02:12
  • 1
    [Jumpin with gravity](http://stackoverflow.com/questions/16493809/how-to-make-sprite-jump-in-java/16494178#16494178) – MadProgrammer Aug 25 '14 at 02:19
  • Try `FloatSpring`, seen [here](http://stackoverflow.com/q/11228554/230513). – trashgod Aug 25 '14 at 03:04
  • the link to the jumpin with gravity helped me and now the problem is solved, thanks @MadProgrammer – user1058860 Aug 25 '14 at 03:46

1 Answers1

0

I'm guessing that actionPerformed() is getting called by some clock? I think the problem is that, within actionPerformed, you've added your own while loop. So, even though actionPerformed only gets called X times per second, that while loop is going to update your variables (time, py, etc.) and repaint the screen as fast as it can. I can't really give you the correct code without understanding the interface you're using (i.e. I don't know where "time" and "py" come from), but maybe try something like this:

    class Jump extends AbstractAction 
    {
        public void actionPerformed(ActionEvent e)
        {
            if (time < 2.5)
            {
                time += 0.1;
                py -= 5-(2*(time));
                if (py > 300) py = 300;
                if (py < 0) py = 0;
                repaint();
            }   

        }
    }
    class Fall extends AbstractAction
    {
        public void actionPerformed(ActionEvent e)
        {
            if (time > 0)
            {
                time -= 0.1;
                py += 5-(2*(time));
                if (py > 300) py = 300;
                if (py < 0) py = 0;
                repaint();
            }

        }
    }
  • the motion is still really choppy, when i use this – user1058860 Aug 25 '14 at 03:09
  • @user1058860 So, rather then having us "guess" a solution for your, consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Aug 25 '14 at 03:15
  • sorry, i've been testing the solutions in the other comments. I just tried this one and wanted to give feedback. I'm still trying other solutions right now. The link to the Jumpin with gravity seems to be helping out, I'm just working out a few kinks right now with it. – user1058860 Aug 25 '14 at 03:18