根据系统时间调用函数

发布于 2024-10-09 06:01:51 字数 1244 浏览 0 评论 0原文

我无法根据系统时间正确调用函数。我的主要目的是为我的“炸弹人”对象提供更好的运动效果。不幸的是,它的行为很奇怪。我相信有一个愚蠢的小错误,但由于30个小时的不眠之夜我找不到。这是我的代码:

                if (System.currentTimeMillis() % 600 <= 200) {
                    if (!gEngine.gMap.bomber.isImmune) {
                        gEngine.gMap.bomber.setImage("bomberman_up3.gif");
                    } else {
                        gEngine.gMap.bomber.setImage("bomberman_red_up3.gif");
                    }
                } else if (System.currentTimeMillis() % 600 <= 400) {
                    if (!gEngine.gMap.bomber.isImmune) {
                        gEngine.gMap.bomber.setImage("bomberman_up2.gif");
                    } else {
                        gEngine.gMap.bomber.setImage("bomberman_red_up2.gif");
                    }


                } else if (System.currentTimeMillis() % 600 >= 400)
                {
                    if (!gEngine.gMap.bomber.isImmune) {
                        gEngine.gMap.bomber.setImage("bomberman_up1.gif");
                    } else {
                        gEngine.gMap.bomber.setImage("bomberman_red_up1.gif");
                    }
                }

顺便说一句,我很确定奇怪的地方不是 gif 图像的顺序。代码应每秒更改图像 3 次。如果你帮助我,也许我可以睡觉。 :)

提前致谢。

I'm having trouble with calling function according to system time correctly. My main purpose is to provide a better movement effect to my "Bomberman" object. Unfortunately, it behaves weird. I believe that there is a stupid little mistake, but I can't find due to sleepless 30 hours. Here is my code:

                if (System.currentTimeMillis() % 600 <= 200) {
                    if (!gEngine.gMap.bomber.isImmune) {
                        gEngine.gMap.bomber.setImage("bomberman_up3.gif");
                    } else {
                        gEngine.gMap.bomber.setImage("bomberman_red_up3.gif");
                    }
                } else if (System.currentTimeMillis() % 600 <= 400) {
                    if (!gEngine.gMap.bomber.isImmune) {
                        gEngine.gMap.bomber.setImage("bomberman_up2.gif");
                    } else {
                        gEngine.gMap.bomber.setImage("bomberman_red_up2.gif");
                    }


                } else if (System.currentTimeMillis() % 600 >= 400)
                {
                    if (!gEngine.gMap.bomber.isImmune) {
                        gEngine.gMap.bomber.setImage("bomberman_up1.gif");
                    } else {
                        gEngine.gMap.bomber.setImage("bomberman_red_up1.gif");
                    }
                }

By the way, I'm pretty sure that the weirdness is not about the order of gif images. Code should change image 3 times per second. Maybe I can sleep if you help me. :)

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚气少女 2024-10-16 06:01:51

假设您使用计时器定期调用该函数,您的特殊问题似乎是您多次请求系统时间,而不是一次,因此您在三个部分中测试的值会发生变化。

您最好创建一个元素队列来利用给定的更新时间,因为如果您只使用大量 if 语句而不是 OO 抽象,您的代码最终会变得越来越复杂。

当绘制屏幕时,获取一次时间,并将该时间传递给所有要绘制的元素。这样,所有元素将彼此同步,并且不会出现第一个 if 的时间与第三个 if 的时间不同的问题>。

因此,我让 BomberMan 实现一个带有所有动画元素都实现的绘制(Graphics2D g,长时间)方法的接口,并具有 get 和 set 方法而不是公共 isImmune 字段。 get 和 set 方法更改精灵图像数组的值,因此代码不需要知道炸弹人所处的任何其他状态。

Assuming you are calling the function periodically with a timer, your particular problem appears to be that you are requesting the system time many times, rather than once, so the value you are testing in the three parts changes.

You would be better off creating a queue of elements to draw on a given update time, as your code will end up more and more complicated if you just use lots of if statements rather than OO abstractions.

When the screen is painted, get the time once, and pass that time to all the elements to be drawn. That way, all the elements will be in synch with each other, and you don't have problems with the time at the first if being different from the time at the third if.

So I'd have BomberMan implement an interface with a draw ( Graphics2D g, long time ) method which all animated elements implement, and have get and set methods instead of a public isImmune field. The get and set methods change the value of an array of images for the sprite, so the code doesn't need to know about any other state the bomber man is in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文