我正在尝试制作一个 2D 相机,它可以垂直和水平地跟随我的玩家。相机仅在水平 atm 后跟随我的播放器。我被告知要在我的方法中更新 Y 部分和 X 部分ScrollCamera()
,但我不知道该怎么做(最终弄得一团糟)。
继承人的代码:
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);
}
这是我的关卡类中的绘制方法:
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);
}
这根本不对。