2012年5月27日 星期日

Java & Slick 遊戲開發(9)-Getting Mouse Input from the User

1.首先 import org.lwjgl.input.Mouse;


2 把字符串成為變量   public String mouse = "No input yet!";


在Menu.class的update function:(當有事要變動時,會執行這個function)



public void update(GameContainer gc,StateBasedGame sbg, int delta) throws SlickException
{
int xpos = Mouse.getX();
int ypos = Mouse.getY();
mouse = "Mouse position x: " + xpos + " y: " + ypos; 
}

Mouse.getX(); --> 獲取畫面的x軸
Mouse.getY(); --> 獲取畫面的y軸
mouse = "Mouse position x: " + xpos + " y: " + ypos; --> 更新mouse內容和印出X同Y軸


*請注意,這個Library的x軸同Y軸是在左下角!但在Render裏的是用Java的library,所以x軸同Y軸是在左上角



全部的代碼:





package javagame;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Menu extends BasicGameState
{
public String mouse = "No input yet!";
public Menu(int State)
{
}
public void init(GameContainer gc,StateBasedGame sbg) throws SlickException
{
}
public void render(GameContainer gc,StateBasedGame sbg, Graphics g) throws SlickException
{
g.drawString(mouse, 50, 50);
g.drawRect(50, 100, 60, 120); //x,y,width,height
Image pic = new Image("res/logo2.png");
g.drawImage(pic, 200, 130);
}
public void update(GameContainer gc,StateBasedGame sbg, int delta) throws SlickException
{
int xpos = Mouse.getX();
int ypos = Mouse.getY();
mouse = "Mouse position x: " + xpos + " y: " + ypos;
}
public int getID()
{
return 0;
}

}










沒有留言:

張貼留言