0
我在代碼中遇到了問題。當我試圖讓玩家和敵人之間發生碰撞時,還有敵人和子彈之間的碰撞,但是它不能。請幫我解決這個問題,非常感謝,在這裏我的代碼:XNA中的碰撞
class Player
{
private Texture2D rightWalk, currentAni, leftwalk, fight, power;
private Vector2 position;
public Rectangle destRect; //destination of rectangle
public Rectangle sourceRect;
float elapsed;
float delay = 200f;
int frames = 0;
private bool hasJumped = false;
private Vector2 velocity;
public Vector2 Position
{
get { return position; }
}
public Player()
{ }
public void Initialize()
{
}
public void Load(ContentManager Content)
{
// Create a new SpriteBatch, which can be used to draw textures.
rightWalk = Content.Load<Texture2D>("saiyan___hero___sprite_sheet_by_luke30800-d3ei6v4");
leftwalk = Content.Load<Texture2D>("2");
fight = Content.Load<Texture2D>("3");
power = Content.Load<Texture2D>("4");
currentAni = rightWalk;
}
public void Animate(GameTime gameTime)
{
elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed >= delay)
{
if (frames >= 2)
{
frames = 0;
}
else
{
frames++;
}
elapsed = 0;
}
sourceRect = new Rectangle(40 * frames, 62, 40, 51);
}
public void Animate1(GameTime gameTime)
{
elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed >= delay)
{
if (frames >= 5)
{
frames = 0;
}
else
{
frames++;
}
elapsed = 0;
}
sourceRect = new Rectangle(68* frames, 62, 68, 51);
}
private void Input(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
currentAni = rightWalk;
velocity.X = (float)gameTime.ElapsedGameTime.TotalMilliseconds/3;
Animate(gameTime);
}
else if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
currentAni = leftwalk;
velocity.X = -(float)gameTime.ElapsedGameTime.TotalMilliseconds/3;
Animate(gameTime);
}
else if (Keyboard.GetState().IsKeyDown(Keys.Space) && hasJumped == false)
{
currentAni = rightWalk;
position.Y -= 5f;
velocity.Y = -9f;
hasJumped = true;
Animate(gameTime);
}
else if(Keyboard.GetState().IsKeyDown(Keys.X))
{
currentAni = fight;
Animate(gameTime);
}
else if (Keyboard.GetState().IsKeyDown(Keys.S))
{
currentAni = power;
Animate1(gameTime);
}
else if (Keyboard.GetState().IsKeyDown(Keys.Z))
{
currentAni = rightWalk;
Animate(gameTime);
}
else
{
velocity.X = 0f;
sourceRect = new Rectangle(0, 62, 40, 52);
}
}
public void Collision(Rectangle newRectangle, int xOffset, int yOffset)
{
if (destRect.Touchtop0f(newRectangle))
{
destRect.Y = newRectangle.Y - destRect.Height;
velocity.Y = 0f;
hasJumped = false;
}
if (destRect.TouchLeft0f(newRectangle))
{
position.X = newRectangle.X - destRect.Width - 2;
}
if (destRect.TouchRight0f(newRectangle))
{
position.X = newRectangle.X + newRectangle.Width + 2;
}
if (destRect.TouchBottom0f(newRectangle))
{
velocity.Y = 1f;
}
if (position.X < 0) position.X = 0;
if (position.X > xOffset - destRect.Width) position.X = xOffset - destRect.Width;
if (position.Y < 0) velocity.Y = 1f;
if (position.Y > yOffset - destRect.Height) position.Y = yOffset - destRect.Height;
}
public void Update(GameTime gameTime)
{
position += velocity;
destRect = new Rectangle((int)position.X, (int)position.Y, 60, 60);
Input(gameTime);
if (velocity.Y < 10)
velocity.Y += 0.4f;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(currentAni,destRect, sourceRect, Color.WhiteSmoke);
}
}
class Enemy
{
Texture2D texture;
Rectangle rectangle;
public Vector2 position;
public Vector2 pos1 = new Vector2(2540, 252);
public Vector2 pos2;
Vector2 origin;
public Vector2 velocity;
public Rectangle sourceRect;
float elapsed;
float delay = 200f;
int frames = 0;
float rotation = 0f;
bool right;
float distance;
float oldDistance;
public Enemy(Texture2D newTexture, Vector2 newPosition, float newDistance)
{
texture = newTexture;
distance = newDistance;
oldDistance = distance;
}
//float mouseDistance;
public void Animate(GameTime gameTime)
{
elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed >= delay)
{
if (frames >= 2)
{
frames = 0;
}
else
{
frames++;
}
elapsed = 0;
}
sourceRect = new Rectangle(40 * frames, 62, 40, 52);
}
public void Update(GameTime gameTime)
{
position += velocity;
pos1 += velocity;
pos2 += velocity;
origin = new Vector2(texture.Width/2, texture.Height/2);
if(distance <= 0)
{
right = true;
velocity.X = 1f;
elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed >= delay)
{
if (frames >= 3)
{
frames = 0;
}
else
{
frames++;
}
elapsed = 0;
}
sourceRect = new Rectangle(40 * frames, 62, 40, 52);
}
else if (distance >= oldDistance)
{
right = false;
velocity.X = -1f;
elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed >= delay)
{
if (frames >= 3)
{
frames = 0;
}
else
{
frames++;
}
elapsed = 0;
}
sourceRect = new Rectangle(40 * frames, 62, 40, 52);
}
if (right)
{
distance += 1 ;
elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed >= delay)
{
if (frames >= 2)
{
frames = 0;
}
else
{
frames++;
}
elapsed = 0;
}
sourceRect = new Rectangle(17 * frames, 05, 18, 27);
}
else
{
distance -= 1;
elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed >= delay)
{
if (frames >= 2)
{
frames = 0;
}
else
{
frames++;
}
elapsed = 0;
}
sourceRect = new Rectangle(17 * frames, 05, 18, 27);
}
}
public void Draw(SpriteBatch spriteBatch)
{
if (velocity.X > 0)
{
spriteBatch.Draw(texture, position, sourceRect, Color.White, rotation, origin, 1f, SpriteEffects.FlipHorizontally, 0f);
spriteBatch.Draw(texture, pos1, sourceRect, Color.White, rotation, origin, 1f, SpriteEffects.FlipHorizontally, 0f);
spriteBatch.Draw(texture, pos2, new Rectangle(0,0,0,0), Color.White, rotation, origin, 1f, SpriteEffects.FlipHorizontally, 0f);
}
else
{
spriteBatch.Draw(texture, position, sourceRect, Color.White, rotation, origin, 1f, SpriteEffects.None, 0f);
spriteBatch.Draw(texture, pos1, sourceRect, Color.White, rotation, origin, 1f, SpriteEffects.None, 0f);
spriteBatch.Draw(texture, pos2, sourceRect, Color.White, rotation, origin, 1f, SpriteEffects.None, 0f);
}
}
}
class Bullet
{
//Bullet
public Texture2D texture;
public bool isvisible;
float elapsed;
float delay = 200f;
int frames = 0;
public Rectangle sourceRect;
public Vector2 position3;
public Bullet(Texture2D newtexture)
{
texture = newtexture;
isvisible = false;
}
public void Animate(GameTime gameTime)
{
elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed >= delay)
{
if (frames >= 5)
{
frames = 0;
}
else
{
frames++;
}
elapsed = 0;
}
sourceRect = new Rectangle(65 * frames, 62, 65, 51);
}
public void update(GameTime gameTime)
{
Animate(gameTime);
}
Game1:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Mapcs map;
Player player;
Camera camera;
Enemy enemy;
Enemy enemy2;
Themestar stars;
List<Bullet> bullets = new List<Bullet>();
List<Enemy> enemys = new List<Enemy>();
Texture2D texture, background;
KeyboardState pastkey;
Bullet bullet;
Color back = Color.Black;
Rectangle enemybound;
Rectangle playerbound;
enum GameState
{
MainMenu,
Option,
Playing,
}
GameState CurrentGameState = GameState.MainMenu;
int screenWidth = 600, screenHight = 800;
cButton btnPlay;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
map = new Mapcs();
player = new Player();
stars = new Themestar();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
graphics.PreferredBackBufferWidth = screenWidth;
graphics.PreferredBackBufferHeight = screenHight;
//graphics.IsFullScreen = true;
//graphics.ApplyChanges();
btnPlay = new cButton(Content.Load<Texture2D>("Button"), graphics.GraphicsDevice);
btnPlay.SetPositon(new Vector2(350, 300));
IsMouseVisible = true;
background = Content.Load<Texture2D>("30xfjo8");
TileMap.Content = Content;
enemy = new Enemy(Content.Load <Texture2D>("enemy"), new Vector2(40, 400),150);
enemy2 = new Enemy(Content.Load<Texture2D>("enemy2"), new Vector2(40, 400), 150);
camera = new Camera(GraphicsDevice.Viewport);
player.Load(Content);
stars.Load(Content);
map.Generate(new int[,]
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,2,2,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,2,2},
{0,0,0,0,0,0,0,0,0,1,1,1,2,2,2,1,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0,1,1,1,2,2,2,1,0,0,0,0,2,2},
{0,0,0,0,0,0,0,0,1,2,2,2,2,2,2,2,1,0,0,0,2,2,0,0,0,0,0,0,0,0,1,2,2,2,2,2,2,2,1,0,0,0,2,2},
{0,0,0,0,0,0,1,1,1,2,2,2,2,2,2,2,2,1,1,1,2,2,0,0,0,0,0,0,1,1,1,2,2,2,2,2,2,2,2,1,1,1,2,2},
{0,0,0,0,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
}, 64);
texture = Content.Load<Texture2D>("bullet");
enemy.position = new Vector2(920, 318);
enemy2.pos2 = new Vector2(2530, 318);
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
MouseState mouse = Mouse.GetState();
switch (CurrentGameState)
{
case GameState.MainMenu:
if (btnPlay.isClicked == true)
CurrentGameState = GameState.Playing;
btnPlay.Update(mouse);
break;
case GameState.Playing:
player.Update(gameTime);
foreach (CollisionTiles tile in map.CollisionTiles)
{
player.Collision(tile.Retangle, map.Width, map.Height);
}
camera.Update(player.Position, map.Width, map.Height);
enemy.Update(gameTime);
enemy2.Update(gameTime);
if (Keyboard.GetState().IsKeyDown(Keys.Z) && pastkey.IsKeyUp(Keys.Z))
{
bullet = new Bullet(texture);
bullet.position3 = new Vector2(player.Position.X + 32, player.Position.Y);
bullets.Add(bullet);
}
pastkey = Keyboard.GetState();
foreach (Bullet bullet in bullets)
{
bullet.position3.X += 6; // velocity of the bullet
bullet.update(gameTime);
}
enemybound = new Rectangle((int)enemy.position.X, (int)enemy.position.Y, 40 , 52);
playerbound = new Rectangle((int)player.destRect.X, (int)player.destRect.Y, 40 , 51);
if (enemybound.Intersects(playerbound))
{
back = Color.Blue;
}
else
{
back = Color.Black;
}
break;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(back);
spriteBatch.Begin(SpriteSortMode.Deferred,
BlendState.AlphaBlend,
null, null, null, null,
camera.Transform);
switch (CurrentGameState)
{
case GameState.MainMenu:
spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
btnPlay.Draw(spriteBatch);
break;
case GameState.Playing:
GraphicsDevice.Clear(Color.Black);
stars.Draw(spriteBatch);
map.Draw(spriteBatch);
enemy.Draw(spriteBatch);
foreach (Bullet bullet in bullets)
{
spriteBatch.Draw(texture,bullet.position3, bullet.sourceRect, Color.White);
}
player.Draw(spriteBatch);
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
}
您好,代碼發佈量是太長時間,你的問題並沒有真正解釋問題。嘗試解釋「爲什麼」你的嘗試不工作,並只發布相關的代碼:) –
謝謝。主要問題在於Game1類。我試圖將玩家和敵人分別添加到兩個矩形中。然後我打電話if(enemybound.Intersects(playerbound)) { back = Color.Blue; } else { back = Color.Black; } break;但沒有任何事情發生,請幫我解決它 –