我一直在尝试为类似游戏的“自上而下视图”创建碰撞。我做了一个玩家类和一个方块类,在我的 Game1 类中我检查了碰撞,不知道这是否是正确的方法,但现在只是一个测试。因此,如果我与它们相交,我不知道下一步该做什么。
这是我的 Game1.cs
using System; using System.Collections.Generic; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Collision_Testing {
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player player = new Player(100, 100);
Block block = new Block(500, 500);
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
block.LoadContent(Content);
player.LoadContent(Content);
}
/// <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();
block.Update(gameTime);
player.Update(gameTime);
base.Update(gameTime);
if (player.boundingBox.Intersects(block.boundingBox))
{
player.playerPos.X -= player.speed;
player.playerPos.Y -= player.speed;
}
}
/// <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);
spriteBatch.Begin();
player.Draw(spriteBatch);
block.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
} }
这是我的 Player.cs
using System; using System.Collections.Generic; using System.Linq;
using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input;
namespace Collision_Testing {
class Player
{
public Vector2 playerPos;
public Texture2D playerTex;
public int speed;
KeyboardState oldState;
public Rectangle boundingBox;
public Player(int positionX, int positionY)
{
playerPos.X = positionX;
playerPos.Y = positionY;
speed = 14;
}
public void LoadContent(ContentManager Content)
{
playerTex = Content.Load<Texture2D>("player");
boundingBox = new Rectangle((int)playerPos.X, (int)playerPos.Y, playerTex.Width, playerTex.Height);
}
public void Update(GameTime gameTime)
{
KeyboardState newState = Keyboard.GetState();
if(newState.IsKeyDown(Keys.Right))
{
playerPos.X += speed;
}
if (newState.IsKeyDown(Keys.Left))
{
playerPos.X -= speed;
}
if (newState.IsKeyDown(Keys.Up))
{
playerPos.Y -= speed;
}
if (newState.IsKeyDown(Keys.Down))
{
playerPos.Y += speed;
}
oldState = newState;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(playerTex, playerPos, Color.White);
}
} }
这是我的 Block.cs
using System; using System.Collections.Generic; using System.Linq;
using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input;
namespace Collision_Testing {
class Block
{
public Vector2 blockPos;
public Texture2D blockTex;
public Rectangle boundingBox;
public Block(int blockX, int blockY)
{
blockPos.X = blockX;
blockPos.Y = blockY;
}
public void LoadContent(ContentManager Content)
{
blockTex = Content.Load<Texture2D>("wall");
boundingBox = new Rectangle((int)blockPos.X, (int)blockPos.Y, blockTex.Width, blockTex.Height);
}
public void Update(GameTime gameTime)
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(blockTex, blockPos, Color.White);
}
} }
我认为我已经为初学者做得很好,但不知道下一步该做什么。