gpt4 book ai didi

c# - XNA 4.0 碰撞检测不工作

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:46 24 4
gpt4 key购买 nike

我已经设法获取我认为需要获取的值,以便在平台类中进行 snape 和平台边界框之间的碰撞检测。但是它不起作用。没有错误,我看不出哪里出错了。非常感谢您的帮助。我的 3 个类(class)如下。

游戏1类

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; }
}
}

}

最佳答案

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();
}

关于c# - XNA 4.0 碰撞检测不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12989898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com