【发布时间】:2014-03-05 13:54:22
【问题描述】:
我正在制作 2D 游戏(不基于图块)并且想让我的相机跟随我的角色。以下是游戏的实现方式:
游戏是基于像素的,所以它是这样的:
如您所见,窗口大小为 840x490,地图为 1260x735。我的 Camera 类保存了相机的 X 和 Y 位置,以及它的大小。代码:
public class Camera{
public Level level;
public GameContainer gc;
public static final int width = Game.displayWidth; // 840
public static final int height = Game.displayHeight; // 490
public static float posX = 0;
public static float posY = 0;
public static int maxX = 0;
public static int maxY = 0;
public Camera(Level level){
this.level = level;
this.maxX = this.level.worldMap.getWidth();
this.maxY = this.level.worldMap.getHeight();
}
}
现在我使用箭头键移动相机(测试),相机的 XY 位置用于g.translate() 图形上下文,然后再绘制其他所有内容,这就是我实现箭头移动的方式。这是我的渲染方法:
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
int offSetX = 10;
int offSetY = 10;
g.translate(-camera.posX, -camera.posY);
worldMap.draw(0 ,0);
for(GameObject o : objects){
o.drawObj(container, game, g);
if(quit == true){
Shape s = o.getBoundsBox();
g.draw(s);
}
}
}
我想要它,这样玩家在靠近任何边缘时总是被摄像机跟踪。但是我怎么能检测到呢?理想情况下,我会在相机内有一个坐标系,并通过检查 X 和 Y 值来检测它,但我找不到在相机窗口内有一个坐标系的方法。
关于如何解决这个问题的任何其他想法?
【问题讨论】: