I am learning about collision detection using rectangles.I created a simple project in which we control the player with arrow keys and if he touches the ball,a new ball will appear.However,a new ball does not appear when I touch the player with the ball.The code:
Main game:
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 WindowsGame4
{
public class Game1 : Microsoft.Xna.Framework.Game
{
public static Texture2D player, ball;
public static GraphicsDeviceManager graphics;
public static SpriteBatch spriteBatch;
public static KeyboardState state;
public static SpriteFont font;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
base.Initialize();
new Ball();
new Player();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
player = Content.Load<Texture2D>("player");
ball = Content.Load<Texture2D>("ball");
font = Content.Load<SpriteFont>("SpriteFont1");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
state = Keyboard.GetState();
Player.Update();
new NewCol();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Ball.Draw();
Player.Draw();
NewCol.Collision();
base.Draw(gameTime);
}
}
}
Ball class:
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 WindowsGame4
{
class Ball
{
private static int width = 500;
private static int height = 300;
public static Vector2 ballloc = new Vector2(width, height);
public Ball()
{ }
public static void Draw()
{
Game1.spriteBatch.Begin();
Game1.spriteBatch.Draw(Game1.ball, ballloc, Color.White);
Game1.spriteBatch.DrawString(Game1.font, "ball width=" + width + "ball height=" + height, new Vector2(0, 0), Color.Black);
Game1.spriteBatch.End();
}
}
}
Player class:
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 WindowsGame4
{
class Player
{
private static int width = 0;
private static int height = 0;
public static Vector2 playerloc = new Vector2(width, height);
public Player()
{ }
public static void Draw()
{ Vector2 playerloc = new Vector2(width, height);
Game1.spriteBatch.Begin();
Game1.spriteBatch.Draw(Game1.player, playerloc, Color.White);
Game1.spriteBatch.DrawString(Game1.font, "player width=" + width + "player height=" + height, new Vector2(50, 50), Color.Black);
Game1.spriteBatch.End();
}
public static void Update()
{ Vector2 playerloc = new Vector2(width, height);
if (Game1.state.IsKeyDown(Keys.Up))
{ height = height - 1; }
if (Game1.state.IsKeyDown(Keys.Down))
{ height = height + 1; }
if (Game1.state.IsKeyDown(Keys.Left))
{ width = width - 1; }
if (Game1.state.IsKeyDown(Keys.Right))
{ width = width + 1; }
}
}
}
Collision Detection class:
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 WindowsGame4
{
class NewCol
{
static Rectangle playerrect = new Rectangle((int)Player.playerloc.X, (int)Player.playerloc.Y, Game1.player.Width, Game1.player.Height);
static Rectangle ballrect = new Rectangle((int)Ball.ballloc.X, (int)Ball.ballloc.Y, Game1.ball.Width, Game1.ball.Height);
public NewCol()
{ }
public static void Collision()
{
if (playerrect.Intersects(ballrect))
{
Game1.spriteBatch.Begin();
Game1.spriteBatch.Draw(Game1.ball, new Vector2(100, 100), Color.White);
Game1.spriteBatch.End();
}
}
}
}
Please tell me what to do and correct the code.
It's very important for me.