目前发生的事情是每当我按空格键时它会翻译然后返回到之前的位置。如何制作它以便在翻译之后保留在该位置然后再次单击空格后再次移动并且不会保留在之前的位置?任何帮助表示赞赏。谢谢。
public void renderchar() {
glPushMatrix();
GL11.glLoadIdentity();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_NORMALIZE);
keypress();
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(100,100);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(100+character.getTextureWidth(),100);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(100+character.getTextureWidth(),100+character.getTextureHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(100,100+character.getTextureHeight());
}
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 2);
while(Keyboard.next()) {
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
System.out.println("SPACE KEY IS DOWN");
GL11.glTranslatef(1.0f, 1.0f, 1.0f);
}
}
glPopMatrix();
GL11.glEnd();
if (totalTime > 500) {
}
}
public void keypress() {
while(Keyboard.next()) {
if(Keyboard.getEventKey() == Keyboard.KEY_SPACE) {
GL11.glTranslatef(100,0,0);
}
}
}
答案 0 :(得分:1)
发生的事情是当你按空格键时,它会翻译对象,下次通过时,如果你的空格键没有被按下,它就不会进行翻译。如果弹出矩阵,则glTranslatef不会递增,因此您需要一个外部变量来存储对象的翻译量。
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
GL11.glTranslatef(x, y, z);
while(Keyboard.next()) {
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
System.out.println("SPACE KEY IS DOWN");
x += 1.0f;
y += 1.0f;
z += 1.0f;
}