java为什么我不能移动我的角色?
我正在尝试制作一个角色可以向上(而不是跳跃)、向左、向右和向下移动的游戏。我是lwjgl的新手,所以如果您发现任何其他错误,或者您有任何建议给我,请不要犹豫
我的代码:
public class Player {
public float x, y, xspeed, yspeed, delta;
public long lastFrame;
public Player(){
x=350;
y=250;
xspeed=1;
}
public void drawPlayer(Texture tex, float x, float y, float width, float height) {
this.x=x;
this.y=y;
tex.bind();
glPushMatrix();
glTranslatef(x,y,0);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0);
glTexCoord2f(1,0);
glVertex2f(width,0);
glTexCoord2f(1,1);
glVertex2f(width,height);
glTexCoord2f(0,1);
glVertex2f(0,height);
glEnd();
glLoadIdentity();
}
public void playerMovement(){
while(Keyboard.next()){
float delta= getDelta();
if(Keyboard.isKeyDown(Keyboard.KEY_D))
xspeed = Math.max(-5, xspeed-1);
xspeed*=0.9;
if (Keyboard.getEventKey()==Keyboard.KEY_W && Keyboard.getEventKeyState())
x++;
xspeed=Math.min(5, xspeed+1);
if (Keyboard.getEventKey()==Keyboard.KEY_A && Keyboard.getEventKeyState())
x++;
xspeed=Math.min(5, xspeed+1);
if (Keyboard.getEventKey()==Keyboard.KEY_S && Keyboard.getEventKeyState())
x++;
xspeed=Math.min(5, xspeed+1);
}
}
public float getDelta(){
long currentTime= getTime();
float delta=(float)(currentTime - lastFrame);
lastFrame=getTime();
return delta;
}
public long getTime(){
return (Sys.getTime() *1000 ) / Sys.getTimerResolution();
}
public void update(int xspeed, int yspeed ){
x+=xspeed;
y+=yspeed;
}
}
# 1 楼答案
我要说的是,如果在球员运动中的陈述是没有意义的
这里,只有第一个赋值取决于键D,而不是乘法。因此,在(!)之后的最后一次X速度分配最后一个if总是“赢”
下面的if语句也是如此