首页 文章

平台上的Java平台游戏土地

提问于
浏览
1

我的Player类中的平台的碰撞代码不起作用,有时会出现故障 . 我创造了不同的应用重力和诸如此类的方法 .

当玩家在平台下并且他们跳跃时,我希望他们与平台的底部碰撞并使用我创建的重力方法倒下 .

当玩家走进平台的两端时,我希望他们停在旁边 .

我想确保玩家无论何时都无法通过平台,这似乎是我的垮台 .

如果有人能帮助我,我将非常感激 .

这是我的Player类,小程序屏幕大小为600x400:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Player implements KeyListener
{
int x, y, width, height;

boolean jump, left, right;

PlayerThread playerThread;

int maxHeight = 40;
double heightC = 0;
boolean onPlatform = false;
boolean landed = true;
int prevY;

public Player()
{
    x = 500;
    y = 350;
    width = 40;
    height = 50;
    playerThread = new PlayerThread();
    playerThread.start();
}

public void paint(Graphics g)
{
    String str = String.valueOf(x);
    String str2 = String.valueOf(y);

    g.setColor(Color.RED);
    g.fillRect(x, y, width, height);

    g.drawString("X: " + str + ", Y: " + str2, 100, 100);
}

public void update(Platform p)
{
    CheckForCollision(p);
}

public void CheckForCollision(Platform p)
{
    int pX = p.getX();
    int pY = p.getY();
    int pWidth = p.getWidth();
    int pHeight = p.getHeight();

    //THIS IS COLLISION DETECTION CODE THAT DOES NOT WORK
    if (y + height > pY && y  < pY + pHeight && x + width > pX && x < pX + pWidth)
    {
        y = prevY;
        landed = true;
    }
}

public class PlayerThread extends Thread implements Runnable
{
    public void run()
    {
        while (true)
        {
            if (left)
            {
                x -= 2;
            }

            if (right)
            {
                x += 2;
            }

            if (jump)
            {
                if (heightC >= maxHeight)
                {
                    System.out.println(heightC);
                    jump = false;
                    heightC = 0;
                }
                else
                {
                    heightC += 1.5;
                    prevY = y;
                    y -= 5;
                    landed = false;
                }
            }

                            //GRAVITY CODE
            if (!jump)
            {
                if (y < 400 - height && !landed)
                {
                    prevY = y;
                    y += 5;
                    landed = false;
                }
            }

            if (y >= 400 - height)
            {
                y = 400 - height;
                landed = true;
            }

            try
            {
                sleep(17);
            } 
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

@Override
public void keyPressed(KeyEvent e)
{
    switch (e.getKeyCode())
    {
    case KeyEvent.VK_LEFT:
        left = true;
        break;
    case KeyEvent.VK_RIGHT:
        right = true;
        break;
    case KeyEvent.VK_SPACE:
        if (landed)
        {
        jump = true;
        }
        break;
    }
}

@Override
public void keyReleased(KeyEvent e)
{
    switch (e.getKeyCode())
    {
    case KeyEvent.VK_LEFT:
        left = false;
        break;
    case KeyEvent.VK_RIGHT:
        right = false;
        break;
    case KeyEvent.VK_SPACE:
        break;
    }
}



public int getX()
{
    return x;
}

public void setX(int x)
{
    this.x = x;
}

public int getY()
{
    return y;
}

public void setY(int y)
{
    this.y = y;
}

public int getWidth()
{
    return width;
}

public void setWidth(int width)
{
    this.width = width;
}

public int getHeight()
{
    return height;
}

public void setHeight(int height)
{
    this.height = height;
}

@Override
public void keyTyped(KeyEvent e)
{
    // TODO Auto-generated method stub

}

}

1 回答

  • 1

    您的问题在于检测代码 . 我假设,您正在使用基于if语句的基本矩形冲突,但是您的子句似乎有点奇怪 .

    让我们分解它们: y + height > pY - 如果播放器底部低于平台顶部 . y < pY + pHeight - 如果播放器顶部高于平台底部 x + width > pX - 如果播放器右侧位于平台左侧 x < pX + pWidth - 播放器左侧位于右侧左侧该平台

    现在请注意这里的逻辑:只有当玩家完全进入平台时才会触发,我希望这不是你想要的 . 你可能想要的更像是否定的证明:

    if(!(y > pY + pHeight || y + height < pY || x > pX + pWidth || x + width < pX)){
        //There is a collision
    }
    

    另外,虽然我一般会同意D先生和他使用游戏引擎的评论,但从学习的角度来看,知道这些东西是如何工作的通常是有用的,所以你不会被限制在别人的代码库中 .

相关问题