简体   繁体   English

Java中的游戏跳转

[英]Game Jump in Java

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();
                }

            }
        }

I'm guessing that actionPerformed() is getting called by some clock? 我猜想actionPerformed()正在被某个时钟调用? I think the problem is that, within actionPerformed, you've added your own while loop. 我认为问题在于在actionPerformed中,您已经添加了自己的while循环。 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. 因此,即使actionPerformed每秒仅被调用X次,该while循环仍将更新您的变量(时间,py等)并尽可能快地重新绘制屏幕。 I can't really give you the correct code without understanding the interface you're using (ie I don't know where "time" and "py" come from), but maybe try something like this: 在不了解您使用的界面的情况下,我无法真正为您提供正确的代码(即,我不知道“时间”和“ py”来自何处),但也许可以尝试以下方法:

    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();
            }

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM