使图像成为“边界”

发布于 2024-11-27 21:01:25 字数 974 浏览 2 评论 0原文

我正在开发一款游戏,其中你是一个发射子弹的简单圆圈及其多人游戏等等。好吧,我试图让边界有点像你必须经历的迷宫类型的东西,我尝试过这样的碰撞检测:

public void checkCollisions(){
    Rectangle r1 = bo.getBounds();
    Rectangle d = p.getBounds();
    if (d.intersects(r1))
        border = true;
}

基本上,如果 border = true 那么我会阻止角色移动。当我这样做时,我有两个问题,

  1. 他不会停止,只是走得很慢。
  2. 即使在边界之外,他仍然处于非常缓慢的状态。

我这样使用边框:

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,

  1. He doesnt stop just goes REALLY slow.
  2. 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 技术交流群。

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

发布评论

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

评论(2

抱猫软卧 2024-12-04 21:01:25

好吧,我仍然认为对游戏逻辑进行全面重组是必要的,但我认为我可以阐明正在发生的事情。让我们看看发生事情的各个地方:

  1. PAINT:在 Swing 线程上,当调用 paint() 时,您会看到是否存在碰撞,如果存在则将速度归零(假设您修复了该问题)如果块)。
  2. KEY:在 Swing 线程上,当按下某个键时,您可以根据按下的键设置速度。
  3. 检查:在某个未知点,您检查是否存在碰撞并记录是否存在碰撞。
  4. 移动:在某个未知的点,你用速度更新你的“人”的位置。

所以问题是:在 Java 中,就像任何其他程序一样,当您按住某个键时,您会收到多个按键事件。第一个和第二个之间会有短暂的延迟,然后它们会快速重复。在浏览器的文本框中尝试一下,那里会发生相同的行为。

那么这对你有什么影响呢?好吧,你可能会遇到这样的场景:

PAINT -> speed set to zero
KEY -> speed set back to -2
MOVE -> guy is moved -2
CHECK -> border = false
PAINT -> speed set to zero again

真的,如果你重构代码,这样你就会得到一个看起来像这样的游戏循环:

public void runGame() {
    while(true) {
        updateSpeeds();
        updatePositionFromSpeed();
        repaint();
    }
}

其中 updateSpeeds() 会查询键是否向下或向上,并计算该人是否可以朝该方向移动,并且 updatePositionFromSpeed() 将更新该人的位置。然后,paint()依赖于该人的 xy 坐标,不会写入它们,并且不需要知道速度。

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:

  1. PAINT: On the Swing thread, when paint() is called, you see if there were collisions and if so zero out the speeds (assuming you fix that if block).
  2. KEY: On the Swing thread, when a key is pressed, you set the speed according to the key pressed.
  3. CHECK: At some unknown point, you check for collisions and record whether there was one.
  4. MOVE: At some unknown point, you update your "guy's" position with the speed.

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:

PAINT -> speed set to zero
KEY -> speed set back to -2
MOVE -> guy is moved -2
CHECK -> border = false
PAINT -> speed set to zero again

Really, if you restructure the code so that you get a game loop that looks something like this:

public void runGame() {
    while(true) {
        updateSpeeds();
        updatePositionFromSpeed();
        repaint();
    }
}

Where updateSpeeds() would instead query whether the key is down or up and also compute whether the guy could move in that direction, and updatePositionFromSpeed() would update the guy's position. Then paint() would rely only on the guy's x and y coordinates, would not write to them, and would not need to know about the speed.

梦旅人picnic 2024-12-04 21:01:25

这是一个非常简单的解决方案。

这是我的一些伪代码。

        if(player.getBounds().intersects(wall.getBounds())){
            //Go Back to prior position, regardless of direction coming from. Since the reverse velocity X and velocity Y directions are taken care off
            x -= velX;
            y -= velY;

            //Then Stop at that prior position to make next move
            velX = 0;
            velY = 0;
        }

here's a very easy solution.

Here's a bit of my pseudo code.

        if(player.getBounds().intersects(wall.getBounds())){
            //Go Back to prior position, regardless of direction coming from. Since the reverse velocity X and velocity Y directions are taken care off
            x -= velX;
            y -= velY;

            //Then Stop at that prior position to make next move
            velX = 0;
            velY = 0;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文