使图像成为“边界”
我正在开发一款游戏,其中你是一个发射子弹的简单圆圈及其多人游戏等等。好吧,我试图让边界有点像你必须经历的迷宫类型的东西,我尝试过这样的碰撞检测:
public void checkCollisions(){
Rectangle r1 = bo.getBounds();
Rectangle d = p.getBounds();
if (d.intersects(r1))
border = true;
}
基本上,如果 border = true 那么我会阻止角色移动。当我这样做时,我有两个问题,
- 他不会停止,只是走得很慢。
- 即使在边界之外,他仍然处于非常缓慢的状态。
我这样使用边框:
boolean border = false;
然后在我的绘制方法中我声明:
if (border)
p.dx = 0;
p.dy = 0;
p 代表 Guy 类:P 更多 dx 和 dy:
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A)
dx = -2;
if (key == KeyEvent.VK_D)
dx = 2;
if (key == KeyEvent.VK_W)
dy = -2;
if (key == KeyEvent.VK_S)
dy = 2;
对于 keyReleased 我只需将 dx 和 dy 的值更改为 0 还有这个人的移动方式:
public void move() {
x = x + dx;
y = y + dy;
}
请帮我弄清楚为什么这不起作用。
I am working on a game in which you are a simple circle that fires bullets and its multiplayer and so on. Well, I am trying to make boundaries sort of like a maze type thing that u have to go through I have tried collision detection like this:
public void checkCollisions(){
Rectangle r1 = bo.getBounds();
Rectangle d = p.getBounds();
if (d.intersects(r1))
border = true;
}
And basically if border = true then i stop the character from moving. I have 2 problems when i do this,
- He doesnt stop just goes REALLY slow.
- He stays at the REALLY slow state even off the border.
I use border like this:
boolean border = false;
then in my paint method i state this:
if (border)
p.dx = 0;
p.dy = 0;
p represents the Guy class :P
More of the dx and dy:
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A)
dx = -2;
if (key == KeyEvent.VK_D)
dx = 2;
if (key == KeyEvent.VK_W)
dy = -2;
if (key == KeyEvent.VK_S)
dy = 2;
and for keyReleased i just change the value of dx and dy to 0
also for how the guy moves:
public void move() {
x = x + dx;
y = y + dy;
}
Please help me figure out why this isn't working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我仍然认为对游戏逻辑进行全面重组是必要的,但我认为我可以阐明正在发生的事情。让我们看看发生事情的各个地方:
paint()
时,您会看到是否存在碰撞,如果存在则将速度归零(假设您修复了该问题)如果块)。所以问题是:在 Java 中,就像任何其他程序一样,当您按住某个键时,您会收到多个按键事件。第一个和第二个之间会有短暂的延迟,然后它们会快速重复。在浏览器的文本框中尝试一下,那里会发生相同的行为。
那么这对你有什么影响呢?好吧,你可能会遇到这样的场景:
真的,如果你重构代码,这样你就会得到一个看起来像这样的游戏循环:
其中
updateSpeeds()
会查询键是否向下或向上,并计算该人是否可以朝该方向移动,并且updatePositionFromSpeed()
将更新该人的位置。然后,paint()
将仅依赖于该人的x
和y
坐标,不会写入它们,并且不需要知道速度。OK, I still think a full restructuring of your game logic is in order, but I think I can shed light as to what's going on. Let's look at the various places where things are happening:
paint()
is called, you see if there were collisions and if so zero out the speeds (assuming you fix that if block).So here's the problem: in Java, just like any other program, you get multiple key pressed events when you're holding down a key. There will be a short delay between the first and second, and then they will repeat rapidly. Try it in a text box in your browser, the same behaviour occurs there.
So how does that affect you? Well, you're probably getting into a scenario like this:
Really, if you restructure the code so that you get a game loop that looks something like this:
Where
updateSpeeds()
would instead query whether the key is down or up and also compute whether the guy could move in that direction, andupdatePositionFromSpeed()
would update the guy's position. Thenpaint()
would rely only on the guy'sx
andy
coordinates, would not write to them, and would not need to know about the speed.这是一个非常简单的解决方案。
这是我的一些伪代码。
here's a very easy solution.
Here's a bit of my pseudo code.