碰撞检测系统我用于我的小游戏工作。它能够分辨出玩家何时直接进入墙壁。然而,当玩家走向对角线时,它会穿过墙壁。我真的不明白问题出在哪里。
以下代码来自处理播放器的类:
public void run()
{
running = true;
while(running)
{
//Get the key/s pressed
controls.update();
//Move down
if (controls.down)
{
movePlayerDown(this.thisPlayer.getSpeed());
}
//Move up
if (controls.up)
{
movePlayerUp(this.thisPlayer.getSpeed());
}
//Move right
if (controls.right)
{
movePlayerRight(this.thisPlayer.getSpeed());
}
//Move left
else if (controls.left)
{
movePlayerLeft(this.thisPlayer.getSpeed());
}
try
{
Thread.sleep(this.sleep);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public void movePlayerRight(int dx)
{
//Gets the tile at what would be the new coordinates of the player
Tile tile = currentLevel.getTile(this.thisPlayer.getX() + dx + this.thisPlayer.getWidth(), this.thisPlayer.getY());
//IF the tile is null or not solid then the player is actually moved
if (tile == null || !tile.isSolid())
{
this.thisPlayer.setX(this.thisPlayer.getX() + dx);
}
}
public void movePlayerLeft(int dx)
{
//Gets the tile at what would be the new coordinates of the player
Tile tile = currentLevel.getTile(this.thisPlayer.getX() - dx, this.thisPlayer.getY());
//IF the tile is null or not solid then the player is actually moved
if (tile == null || !tile.isSolid())
{
this.thisPlayer.setX(this.thisPlayer.getX() - dx);
}
}
public void movePlayerDown(int dy)
{
//Gets the tile at what would be the new coordinates of the player
Tile tile = currentLevel.getTile(this.thisPlayer.getX(), this.thisPlayer.getY() + dy + this.thisPlayer.getHeight());
//IF the tile is null or not solid then the player is actually moved
if (tile == null || !tile.isSolid())
{
this.thisPlayer.setY(this.thisPlayer.getY() + dy);
}
}
public void movePlayerUp(int dy)
{
//Gets the tile at what would be the new coordinates of the player
Tile tile = currentLevel.getTile(this.thisPlayer.getX(), this.thisPlayer.getY() - dy);
//IF the tile is null or not solid then the player is actually moved
if (tile == null || !tile.isSolid())
{
this.thisPlayer.setY(this.thisPlayer.getY() - dy);
}
}
下一个代码块是getTile()方法:
public Tile getTile(int x, int y)
{
for (int r = 0; r < worldTiles.length; r++)
{
for (int c = 0; c < worldTiles[r].length; c++)
{
if (worldTiles[r][c] != null)
{
int right = worldTiles[r][c].getX() + worldTiles[r][c].getWidth();
int bottom = worldTiles[r][c].getY() + worldTiles[r][c].getHeight();
if (x > worldTiles[r][c].getX() && x < right && y > worldTiles[r][c].getY() && y < bottom)
{
return worldTiles[r][c];
}
}
}
}
答案 0 :(得分:0)
a)你没有定义tile == null的条件 - 这是为了进一步考虑
b)测试每个事件的逻辑如下 - 一次一个键
library(shiny)
ui <- fluidPage(
textOutput("text_1")
textOutput("text_2")
)
server <- function(input, output) {
output$text_1<- renderText({ "Hi" }) #standard
assign(paste0("output$text_",2), renderText({ "Hello" }) ) #sth. I would like to do
}
shinyApp(ui = ui, server = server)
睡眠应该从那里移除,因为它会阻碍程序并可能丢失事件。
c)事件应该从关键事件处理程序触发,而不是有一个去那里并获取它们。
事实上,我会将整个逻辑反转如下:
//Move down
if (down)
{
movePlayerDown(this.thisPlayer.getSpeed());
}
//Move up
else if (up)
{
movePlayerUp(this.thisPlayer.getSpeed());
}
//Move right
else if (right)
{
movePlayerRight(this.thisPlayer.getSpeed());
}
//Move left
else if (left)
{
movePlayerLeft(this.thisPlayer.getSpeed());
}
其中
void keyPressed(event) {
sendEvent(event)
}
替换你的while循环。