如何获得鼠标位置的精灵坐标?游戏引擎Slick2d。平铺地图16x16。
相机
public class Camera {
public int x, y;
Object object;
public Camera(int x, int y)
{
this.x = x;
this.y = y;
}
public void tick()
{
if(object != null)
{
if(object instanceof Player)
{
Player player = (Player) object;
this.x = player.getX() - Jail.getJail().gm.getWidth() / 2;
this.y = player.getY() - Jail.getJail().gm.getHeight() / 2;
}
else if(object instanceof Item)
{
Item item = (Item) object;
this.x = item.getX() - Jail.getJail().gm.getWidth() / 2;
this.y = item.getY() - Jail.getJail().gm.getHeight() / 2;
}
else if(object instanceof Vector2)
{
Vector2 vector = (Vector2) object;
this.x = (int) vector.getX() - Jail.getJail().gm.getWidth() / 2;
this.y = (int) vector.getY() - Jail.getJail().gm.getWidth() / 2;
}
else
{
System.out.println("Unknown object.");
}
}
else
{
System.out.println("Camera null object.");
}
}
public void setObject(Object object)
{
this.object = object;
}
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
}
鼠标单击
for (int l = 0; l < Jail.itemlist.size(); ++l)
{
Item item = (Item)Jail.itemlist.get(l);
jail.cam.setObject(new Vector2(Mouse.getEventX(), Mouse.getEventY()));
System.out.println("Mouse x: " + jail.cam.getX() / 16);
System.out.println("Mouse y: " + jail.cam.getY() / 16);
System.out.println("Item x: " + item.getX() / 16);
System.out.println("Item y: " + item.getY() / 16);
if (item.mousePressed(this.jail, par1, par2))
{
System.out.println("CLICK ON ITEM");
}
else
{
System.out.println("CLICK NO ITEM");
}
jail.cam.setObject(jail.player);
}
在不同的位置玩家,不同的鼠标坐标。如何解决这个问题?
鼠标x:8 鼠标y:5 项目x:10 项目y:10 球员x:37 球员y:23
鼠标x:9 鼠标y:-5 项目x:10 项目y:10 球员x:37 球员y:13
鼠标x:-2 鼠标y:-5 项目x:10 项目y:10 球员x:25 球员y:13
答案 0 :(得分:0)
我在这里猜测,可能是你的Mouse.getEventX(), Mouse.getEventY()
给出了代表窗口中mousePosition的坐标。而玩家坐标代表世界中的位置,从(0/0)开始到(tilesX * 16 / tilesY * 16)。
由于我需要查看更多代码,因此Slick2d translate to worldcoordinates可以帮助您。