我的游戏类中平台的碰撞代码不起作用,有时会出故障。我发明了不同的方法来应用重力之类的东西。
当玩家在平台下跳跃时,我想让他们与平台底部相撞,然后用我创造的重力法倒地。
当玩家走进平台的两端时,我想让他们停在它旁边。
我想确保球员在任何时候都不能通过平台,这似乎是我的失败。
如果有人能帮我的话我会很感激的。
这是我的播放器类,applet屏幕大小为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
}}
发布于 2013-04-21 02:41:26
你的问题是你的检测代码。我猜想,您使用的是基于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先生和他关于使用游戏引擎的意见,但从学习的角度来看,了解这些东西是如何工作的往往是有用的,这样你就不会受制于某个人的代码库了。
https://stackoverflow.com/questions/16126810
复制相似问题