我正在尝试制作一个垂直和水平方向跟随我的播放器的2D相机。相机仅在水平方向上跟随我的播放器。我被告知在我的ScrollCamera()
方法中更新Y部分以及X部分,但我无法弄清楚如何做到这一点(最终导致一团糟)。
下面是代码:
private void ScrollCamera(Viewport viewport)
{
#if ZUNE
const float ViewMargin = 0.45f;
#else
const float ViewMargin = 0.5f;
#endif
// Calculate the edges of the screen.
float marginWidth = viewport.Width * ViewMargin;
float marginLeft = cameraPosition + marginWidth;
float marginRight = cameraPosition + viewport.Width - marginWidth;
// Calculate how far to scroll when the player is near the edges of the screen.
float cameraMovement = 0.0f;
if (Player.Position.X < marginLeft)
cameraMovement = Player.Position.X - marginLeft;
else if (Player.Position.X > marginRight)
cameraMovement = Player.Position.X - marginRight;
// Update the camera position, but prevent scrolling off the ends of the level.
float maxCameraPositionWidth = Tile.Width * Width - viewport.Width;
cameraPosition = MathHelper.Clamp(cameraPosition + cameraMovement, 0.0f, maxCameraPositionWidth);
}
这是我的关卡类中的draw方法:
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Begin();
for (int i = 0; i <= EntityLayer; ++i)
layers[i].Draw(spriteBatch, cameraPosition);
spriteBatch.End();
ScrollCamera(spriteBatch.GraphicsDevice.Viewport);
Matrix cameraTransform = Matrix.CreateTranslation(-cameraPosition, 0.0f, 0.0f);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default,
RasterizerState.CullCounterClockwise, null, cameraTransform);
DrawTiles(spriteBatch);
Player.Draw(gameTime, spriteBatch);
foreach (Enemy enemy in enemies)
enemy.Draw(gameTime, spriteBatch);
spriteBatch.End();
spriteBatch.Begin();
for (int i = EntityLayer + 1; i < layers.Length; ++i)
layers[i].Draw(spriteBatch, cameraPosition);
spriteBatch.End();
}
我是Xna和C#的新手,我这样做只是为了好玩,但如果有人能告诉我该怎么做的话会很棒:)
我试过了:
private void ScrollCamera(Viewport viewport)
{
#if ZUNE
const float ViewMargin = 0.45f;
#else
const float ViewMargin = 0.5f;
const float ViewHeight = 0.3f;
#endif
// Calculate the edges of the screen.
float marginHeight = viewport.Height * ViewHeight;
float marginTop = cameraPosition + marginHeight;
float marginBottom = cameraPosition + marginHeight;
float marginWidth = viewport.Width * ViewMargin;
float marginLeft = cameraPosition + marginWidth;
float marginRight = cameraPosition + viewport.Width - marginWidth;
// Calculate how far to scroll when the player is near the edges of the screen.
float cameraMovement = 0.0f;
if (Player.Position.X < marginLeft)
cameraMovement = Player.Position.X - marginLeft;
else if (Player.Position.X > marginRight)
cameraMovement = Player.Position.X - marginRight;
if (Player.Position.Y < marginTop)
cameraMovement = Player.Position.Y - marginTop;
else if (Player.Position.Y > marginBottom)
cameraMovement = Player.Position.X - marginBottom;
// Update the camera position, but prevent scrolling off the ends of the level.
float maxCameraPositionWidth = Tile.Width * Width - viewport.Width;
cameraPosition = MathHelper.Clamp(cameraPosition + cameraMovement, 0.0f, maxCameraPositionWidth);
}
这根本不对。
答案 0 :(得分:2)
从您提供的代码中我得出结论'cameraPosition'是一个浮点数,您只能跟踪X位置。我建议使用Vector2。这在技术上是两个浮在一个;一个用于X位置,一个用于Y.如果执行此操作,则必须重复计算Y值。如果您不想使用Vector2,则可以为Y位置创建第二个变量。
我无法看到你对cameraPosition做了什么,所以我无法帮助你处理Y部分。
修改强> 您所做的绘图功能不正确,因为相机位置只有一个变量。 X位置需要一个变量,Y位置需要一个变量。如上所述,在XNA中执行此操作的典型方法是通过Vector2结构。如果您不熟悉结构(或类),我建议尽快阅读它,因为它们在面向对象编程中非常重要。 Microsoft Office2文档的链接:http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector2.aspx
您基本上要做的是先进行计算。
float cameraMovement = 0.0f;
if (Player.Position.X < marginLeft)
cameraMovement = Player.Position.X - marginLeft;
else if (Player.Position.X > marginRight)
cameraMovement = Player.Position.X - marginRight;
// Update the camera position, but prevent scrolling off the ends of the level.
float maxCameraPositionWidth = Tile.Width * Width - viewport.Width;
cameraPosition = MathHelper.Clamp(cameraPosition + cameraMovement, 0.0f, maxCameraPositionWidth);
这是相机X位置的计算。使用Vector2的代码是:
// Declare a Vector2 cameraPosition where you are now declaring a float cameraPosition right now.
Vector2 cameraMovement = new Vector2(0,0);
if (Player.Position.X < marginLeft)
cameraMovement.X = Player.Position.X - marginLeft;
else if (Player.Position.X > marginRight)
cameraMovement.X = Player.Position.X - marginRight;
float maxCameraPositionWidth = Tile.Width * Width - viewport.Width;
cameraPosition.X = MathHelper.Clamp(cameraPosition.X + cameraMovement.X, 0.0f, maxCameraPositionWidth);
您必须对cameraMovement.Y和cameraPosition.Y进行类似的计算。
当你拥有这些时,你在绘制功能中唯一需要改变的是Y平移。目前你有:
Matrix cameraTransform = Matrix.CreateTranslation(-cameraPosition, 0.0f, 0.0f);
如您所见,您只是在X轴上进行翻译。添加Y轴后,它看起来像这样:
Matrix cameraTransform = Matrix.CreateTranslation(-cameraPosition.X, -cameraPosition.Y, 0.0f);
我希望你现在明白这一点。祝你好运!