0
我試圖讓一個正方形跟着我的鼠標,我離開後點擊它。當我右擊廣場時,應該停止跟隨我的鼠標。試圖用鼠標移動一個四/四方形 - LWJGL
我的程序檢測到im在方塊內部單擊,但由於某種原因,它不會根據Mouse.getDX/DY更新其位置。
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class SimpleOGLRenderer {
private static boolean somethingIsSelected = false;
public static void main(String args[]) {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.setTitle("Hello World");
Display.create();
} catch (LWJGLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Initializare OPENGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 600, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
while(!Display.isCloseRequested())
{
//Render
glClear(GL_COLOR_BUFFER_BIT);
Box box = new Box(100,100);
if(Mouse.isButtonDown(0) && box.inBounds(Mouse.getX(), Display.getHeight()-Mouse.getY()-1) && !somethingIsSelected)
{
System.out.println("Box clicked");
somethingIsSelected = true;
box.selected = true;
}
if(Mouse.isButtonDown(1))
{
box.selected = false;
somethingIsSelected = false;
System.out.println("Box released");
}
if(box.selected)
{
box.update(Mouse.getDX(), -Mouse.getDY());
}
box.drawQuad();
Display.update();
// Display.sync(60);
}
Display.destroy();
}
private static class Box{
public int x, y;
public boolean selected=false;
Box(int x, int y) {
this.x = x;
this.y = y;
}
void drawQuad()
{
glBegin(GL_QUADS);
glVertex2i(x,y);
glVertex2i(x+50,y);
glVertex2i(x+50,y+50);
glVertex2i(x,y+50);
glEnd();
}
void update(int dx,int dy)
{
x = x + dx;
y = y + dy;
}
boolean inBounds(int mouseX, int mouseY) {
return mouseX > x && mouseX < x + 50 && mouseY > y && mouseY < y + 50;
}
}
}
您是否嘗試過使用調試器來查看發生了什麼? –
不,因爲我剛剛切換到Eclipse,並且我對使用調試器相當陌生。我試圖通過在代碼的某些關鍵階段打印某些消息來進行自己的調試。 –