我的碰撞检测代码有效,但是一旦我将玩家和怪物的速度设置为大于 1,我就会得到奇怪的结果,我该如何解决这个问题?
速度为 1
速度为 4
我的碰撞检测:
static boolean collisionDown(Entity e)
{
for(Block i : Game.blocks)
{
Rectangle player = new Rectangle(e.getX(), e.getY() + e.getVelocity(), e.getWidth(), e.getHeight());
Rectangle block = new Rectangle(i.getX(), i.getY(), size, size );
if (player.intersects(block))
{
//e.goUp(1);
return true;
}
}
return false;
}
static boolean collisionUp(Entity e)
{
for(Block i : Game.blocks)
{
Rectangle player = new Rectangle(e.getX(), e.getY() - e.getVelocity(), e.getWidth(), e.getHeight());
Rectangle block = new Rectangle(i.getX(), i.getY(), size, size);
if (player.intersects(block))
{
return true;
}
}
return false;
}
static boolean collisionRight(Entity e)
{
for(Block i : Game.blocks)
{
Rectangle player = new Rectangle(e.getX() + e.getVelocity(), e.getY(), e.getWidth(), e.getHeight());
Rectangle block = new Rectangle(i.getX(), i.getY(), size, size);
if (player.intersects(block))
{
return true;
}
}
return false;
}
static boolean collisionLeft(Entity e)
{
for(Block i : Game.blocks)
{
Rectangle player = new Rectangle(e.getX() - e.getVelocity(), e.getY(), e.getWidth(), e.getHeight());
Rectangle block = new Rectangle(i.getX(), i.getY(), size, size);
if (player.intersects(block))
{
return true;
}
}
return false;
}
我在哪里使用碰撞检测
void goUp(int v)
{
if(!Block.collisionUp(this))
y -= v;
}
void goDown(int v)
{
if(!Block.collisionDown(this))
y += v;
}
void goRight(int v)
{
if(!Block.collisionRight(this))
x += v;
}
void goLeft(int v)
{
if(!Block.collisionLeft(this))
x -= v;
}
谢谢