【发布时间】:2015-07-21 09:52:50
【问题描述】:
我正在开发一款 2D 游戏,现在我面临一个问题,如果不解决它就无法继续。在我的项目中,我首先绘制不随相机移动的东西,而不是总是停留在同一个地方的东西(主要是 UI 元素)。我的游戏内鼠标矩形设置为鼠标位置 + X 和 Y 中的相机中心。
所以,虽然玩家可以点击 UI 元素(我现在在鼠标和按钮之间使用 Intersects 功能,但我会将 UI 上的大部分工作留到最后),如果我尝试执行任何操作鼠标与实际随相机移动的任何物体之间的交互,它不起作用,因为鼠标的坐标与相机“比较”,而对象不是,我不知道如何使它工作。
这是相机的代码:
class Camera
{
public Matrix transform;
Viewport view;
Vector2 centre;
int x;
int y;
float zoom;
public Camera(Viewport newView)
{
view = newView;
x = 0;
y = 0;
zoom = 1;
}
public Vector2 getCentre()
{
return this.centre;
}
public void Update(GameTime gametime)
{
if (Mouse.GetState().Y < 45 && y >= 5)
y -= 3;
if (Mouse.GetState().Y > 400 && y < 650)
y += 3;
if (Mouse.GetState().X > 580 && x < 90)
x += 3;
if (Mouse.GetState().X < 60 && x > -100)
x -= 3;
centre = new Vector2(x, y);
transform = Matrix.CreateScale(new Vector3(1,1,0)) * Matrix.CreateTranslation(new Vector3(-centre.X,-centre.Y,0));
}
}
【问题讨论】: