所以,我正在写一个小游戏。我在JFrame中有一个JPanel,JFrame每帧都在游戏引擎类中重新绘制,但是玩家精灵不会对按键事件作出反应。
有任何帮助吗? 哦,我不完全确定要发布什么代码,因为我似乎无法以任何方式找到问题的根源,所以只需告诉我您希望看到的代码。
引擎中的运行方法:
@Override
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 60D;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
init();
while (engineRunning) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while (delta >= 1) {
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (shouldRender) {
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer += 1000;
frames = 0;
ticks = 0;
}
}
}
Tick&在引擎类中渲染:
public void tick() {
for (BaseEntity e : worldEntities) {
e.tick();
}
}
public void render() {
frame.repaint();
}
玩家打勾:
public void tick() {
Position.X += Position.velX;
Position.Y += Position.velY;
if (Position.velX > Max_Speed)
Position.velX = Max_Speed;
if (Position.velY > Max_Speed)
Position.velY = Max_Speed;
}