带有水平滚动框和碰撞的XNA 2D相机
我一直在制作XNA和2D游戏,但我有问题 我的精灵正在跳过屏幕上滚动的盒子。精灵后面跟着一个2D摄像机,我担心摄像机会引起问题,因为它会导致滚动框在屏幕的一半停止,而不是继续,并且当一次碰撞发生时,生命数量正在迅速减少,而不是一次生命 这是我的精灵与移动框碰撞的代码带有水平滚动框和碰撞的XNA 2D相机,xna,camera,scroll,2d,collision-detection,Xna,Camera,Scroll,2d,Collision Detection,我一直在制作XNA和2D游戏,但我有问题 我的精灵正在跳过屏幕上滚动的盒子。精灵后面跟着一个2D摄像机,我担心摄像机会引起问题,因为它会导致滚动框在屏幕的一半停止,而不是继续,并且当一次碰撞发生时,生命数量正在迅速减少,而不是一次生命 这是我的精灵与移动框碰撞的代码 Rectangle fairyRectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
Rectangle fairyRectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
hit = false;
for (int i = 0; i < GameConstants.TotalBoxes; i++)
{
if (scrollingBlocks.boxArray[i].alive)
{
Rectangle blockRectangle = new Rectangle((int)scrollingBlocks.boxArray[i].position.X, (int)scrollingBlocks.boxArray[i].position.Y, scrollingBlocks.boxArray[i].texture.Width, scrollingBlocks.boxArray[i].texture.Height);
if (IntersectPixels(fairyRectangle, fairyTextureData, blockRectangle, blockTextureData))
{
scrollingBlocks.boxArray[i].alive = false;
hit = true;
lives--;
scrollingBlocks.boxArray[i].alive = true;
scrollingBlocks.boxArray[i].position.X = random.Next(GameConstants.ScreenWidth);
scrollingBlocks.boxArray[i].position.Y = 570;
}
}
}
谢谢你的帮助 从提供的代码中,我不能很好地说明您的相机是如何工作的,但我的猜测是,当您启动关卡时,您的相机会选择一个新的中心点(而不是原始xna屏幕位置) 换句话说,相机的中心可能会将其向后推一点,因此您认为屏幕的位置0可能会被向前推到屏幕的中间,使方框停止。我建议看一下这些相机教程或者 相机可能很棘手,所以我建议看一些教程来获得悬挂矩阵和视图端口 至于夺走不止一条生命,这是因为你说“如果这个盒子与我的播放器相撞,夺走生命。”计算机不知道你只想夺走一条生命,只要它与播放器相撞,它就会不断减少 希望这能给你一些想法:D
for (int i = 0; i < GameConstants.TotalBoxes; i++)
{
boxArray[i].position.X = boxArray[i].position.X - 5;
boxArray[i].position.Y = boxArray[i].position.Y;
if (boxArray[i].position.X < 0)
{
boxArray[i].position.X = randomno.Next(GameConstants.ScreenWidth) + 700;
boxArray[i].position.Y = 570;
}
Helper.WrapScreenPosition(ref boxArray[i].position);
}
}
if (gameState == GameState.PLAYINGLEVEL3)
{
graphics.GraphicsDevice.Clear(Color.Aquamarine);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
backgroundManager.Draw(spriteBatch);
//scrollingBlocks.Draw(spriteBatch);
spriteBatch.DrawString(lucidaConsole, "Score: " + score + " Level: " + level + " Time Remaining: " + ((int)timer / 1000).ToString() + " Lives Remaining: " + lives, scorePosition, Color.DarkOrchid);
spriteBatch.End();
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.None, camera.transform);
scrollingBlocks.Draw(spriteBatch);
fairyL3.Draw(spriteBatch);
spriteBatch.End();
}