我设法获得我认为他们需要获取的值,以便snape和平台边界框之间的碰撞检测发生在平台类中。但它不起作用。没有错误,我无法看到我出错的地方。 非常感谢您的帮助。我的3个课程在下面。
Game1 Class
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D background;
Movement character;
Platform[] platforms;
//private Vector2 SnapePosition = Vector2.Zero;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 440;
graphics.PreferredBackBufferWidth = 782;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
platforms = new Platform[15];
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
character = new Movement(Content.Load<Texture2D>("snape"), new Rectangle(0, 350, 50, 50));
for (int i = 0; i < platforms.Length; i++)
{
platforms[i] = new Platform(
Content.Load<Texture2D>("Platforms/lvl2_platform"), new Rectangle(i*100, 410, 100, 30), character.Snape, character.SnapePosition);
}
// TODO: use this.Content to load your game content here
background = Content.Load<Texture2D>("Backgrounds/lvl2_background");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//Allows the player to move
character.Update();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(background, Vector2.Zero, Color.White);
character.Draw(spriteBatch);
foreach (Platform platform in platforms)
{
platform.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
玩家类
class Player
{
public Texture2D Snape;
public Rectangle SnapePosition;
public virtual void Update()
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Snape,SnapePosition,Color.White);
}
}
class Movement : Player
{
public Movement(Texture2D newSnape, Rectangle newSnapePosition)
{
Snape = newSnape;
SnapePosition = newSnapePosition;
}
public override void Update()
{
KeyboardState keyBoard = Keyboard.GetState();
if (keyBoard.IsKeyDown(Keys.A))
{
SnapePosition.X -= 5;
}
if (keyBoard.IsKeyDown(Keys.D))
{
SnapePosition.X += 5;
}
if (keyBoard.IsKeyDown(Keys.W))
{
SnapePosition.Y -= 5;
}
if (keyBoard.IsKeyDown(Keys.S))
{
SnapePosition.Y += 5;
}
}
}
}
平台类
class Platform
{
Texture2D texture;
Rectangle rectangle;
Texture2D snape;
Rectangle snapePosition;
public Rectangle test;
public enum CollisionPosition { None, Top, Bottom, Left, Right };
public CollisionPosition collisionType;
public bool inCollision;
public int collisionDepth;
public Platform(Texture2D newTexture, Rectangle newRectangle, Texture2D newSnape, Rectangle newSnapePos)
{
texture = newTexture;
rectangle = newRectangle;
snapePosition = newSnapePos;
snape = newSnape;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, rectangle, Color.White);
}
public void Collisions()
{
if (rectangle.Intersects(snapePosition))
inCollision = true;
}
public void DetermineCollisionType()
{
if (inCollision == false)
{
collisionType = CollisionPosition.None;
collisionDepth = 0;
}
else
{
// Determine the side of *least intersection* for snape
int minOverlap = int.MaxValue;
// Check the top side
int tOverlap =
(rectangle.Y + texture.Height / 2)
- (snapePosition.Y - snape.Height / 2);
if (tOverlap > 0 && tOverlap < minOverlap)
{
collisionType = CollisionPosition.Top;
minOverlap = tOverlap;
}
// Check the bottom side
int bOverlap =
(snapePosition.Y + snape.Height / 2)
- (rectangle.Y - texture.Height / 2);
if (bOverlap > 0 && bOverlap < minOverlap)
{
collisionType = CollisionPosition.Bottom;
minOverlap = bOverlap;
}
// Check the right overlap
int rOverlap =
(snapePosition.X + snape.Width / 2)
- (rectangle.X - texture.Width / 2);
if (rOverlap > 0 && rOverlap < minOverlap)
{
collisionType = CollisionPosition.Right;
minOverlap = rOverlap;
}
// Check the left overlap
int lOverlap =
(rectangle.X + texture.Width / 2)
- (snapePosition.X - snape.Width / 2);
if (lOverlap > 0 && lOverlap < minOverlap)
{
collisionType = CollisionPosition.Left;
minOverlap = lOverlap;
}
// Update the collision depth
collisionDepth = minOverlap;
}
}
public void SeparateSnape()
{
switch (collisionType)
{
case CollisionPosition.None:
break;
case CollisionPosition.Top:
snapePosition.Y += collisionDepth;
break;
case CollisionPosition.Bottom:
snapePosition.Y -= collisionDepth;
break;
case CollisionPosition.Right:
snapePosition.X -= collisionDepth;
break;
case CollisionPosition.Left:
snapePosition.X += collisionDepth;
break;
}
}
public void Update()
{
// Check for collision
Collisions();
// Determine collision type
DetermineCollisionType();
// Separate snape
SeparateSnape();
}
public Rectangle getSnapePos
{
get { return snapePosition; }
}
}
}
答案 0 :(得分:1)
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//Allows the player to move
character.Update();
// TODO: Add your update logic here
base.Update(gameTime);
}
我没有看到有关您的平台的任何更新。
尝试添加
foreach (Platform platform in platforms)
{
platform.Update();
}
答案 1 :(得分:1)
您的部分问题似乎是您不知道类和结构之间的区别。矩形是一个结构,这意味着当你将它作为参数传递给任何方法时,它会被“按值”传递,这意味着该值的副本会生成并且只要该方法正在运行就会存在,之后它就不再存在了存在。另一方面,类是引用类型,因此如果您将一个类作为参数传递,那么您将通过“引用”传递它,这意味着该方法将获得一个值,告诉它在何处访问该类。这里的区别在于,尽管在方法结束时引用被破坏,但它只是一个引用,而不是值本身,与结构不同。
除此之外,即使你给平台提供了snape的位置而不仅仅是一个值,当你移动snape时,你改变了他的位置的整个值,而不仅仅是它的一部分,所以它会是覆盖snape的参考资料,无论如何都要使平台的参考文献过时。
老实说,我认为你最好重新思考一下你的设计。 首先,尝试将键盘管理移到角色之外,这样游戏基本上会挂在一个角色上并控制那个角色的移动。这很重要,因为它意味着游戏本身知道主角是谁,角色可以碰撞的平台,因此可以与平台和角色进行碰撞检查。如果平台无法改变角色的位置并且玩家不知道地图上的所有平台是什么,那么让游戏本身监控它似乎更为明智。
以下是一个例子:
protected override void Update()
{
KeyboardState keyboardState = Keyboard.GetState();
Rectangle oldBounds = player.Bounds;
if (keyboardState.IsKeyDown(Keys.W)) { player.Y--; }
if (keyboardState.IsKeyDown(Keys.A)) { player.X--; }
if (keyboardState.IsKeyDown(Keys.X)) { player.Y++; }
if (keyboardState.IsKeyDown(Keys.D)) { player.X++; }
foreach (Platform platform in platforms)
{
if (player.Bounds.Intersects(platform.Bounds))
{
player.Bounds = oldBounds;
break;
}
}
}
你会想让它更复杂,但这可能是最基本的碰撞系统。显然,如果你正在做一个平台游戏,你可能会想要更聪明一些。我建议你尽可能多地进行研究和实验,同时确保查找如何在代码中添加断点并逐行运行代码,以及使用本地和监视框来确切地知道发生了什么代码运行时如果您能够确定正在发生的事情以及应该发生的事情,那么它将使事情变得更容易修复和理解。