Now I have a new one and I have no idea how to solve it.
I put in a bullet class, and a new sprite called ring. The ring is around the "avatar" and the bullet will fire the way I aim.
The aiming part is okey, the ring is locked on the avatar and all that is good, but everytime I try to shoot I get an error message saying what the topic says. And like I said, have no clue.
What have I missed?
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 RotateTowardsMouse
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Vector2 mousePosition;
Player player = null;
List<Bullet> bullets = new List<Bullet>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.IsMouseVisible = true;
}
protected override void Initialize()
{
Window.Title = "alpha" ;
graphics.PreferredBackBufferWidth = 1024;
graphics.PreferredBackBufferHeight = 768;
graphics.ApplyChanges();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
player = new Player(spriteBatch, this);
player.ScreenBoundaries = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
player.Initialize();
player.SetBulletList(ref bullets);
}
protected override void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Escape))
{
this.Exit();
}
player.Update(gameTime);
List<Bullet> bulletKillList = new List<Bullet>();
foreach (Bullet bullet in bullets)
{
bullet.Update(gameTime);
if (bullet.killMe)
{
bulletKillList.Add(bullet);
}
}
foreach (Bullet bullet in bulletKillList)
{
bullets.Remove(bullet);
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
player.Draw(gameTime);
foreach (Bullet bullet in bullets)
{
bullet.Draw(gameTime);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Entity.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace RotateTowardsMouse
{
class Entity : DrawableGameComponent
{
public Vector2 position = Vector2.Zero;
public float speed = 0.0f;
public Vector2 ringPosition = Vector2.Zero;
public Vector2 playerRotationPoint;
public float playerRotation;
public float ringRotation;
public Vector2 ringRotationPoint;
public Vector2 velocity = Vector2.Zero;
public Vector2 mousePosition;
public Vector2 origin = Vector2.Zero;
public bool killMe = false;
protected Texture2D texture;
protected Texture2D ringTexture;
public SpriteBatch spriteBatch;
public Rectangle screenBoundaries;
public Rectangle ScreenBoundaries
{
get { return screenBoundaries; }
set { screenBoundaries = value; }
}
public Entity(SpriteBatch spritebatch, Game game)
: base(game)
{
this.spriteBatch = spritebatch;
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(texture, position, null, Color.White, playerRotation, playerRotationPoint, 1.0f, SpriteEffects.None, 0.0f);
//When I try to shoot the compiler give me an error saying that "This method does not accept null for this parameter".
spriteBatch.Draw(ringTexture, position, null, Color.White, ringRotation, ringRotationPoint, 1.0f, SpriteEffects.None, 0.0f);
spriteBatch.End();
}
}
}
Player.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace RotateTowardsMouse
{
class Player : Entity
{
public int shootTime = 500;
public int shootTimeLeft = 0;
public float maxSpeed = 1.0f;
public float rotationSpeed = 0.003f;
public float acceleration = 0.02f;
private List<Bullet> bullets = null;
public void SetBulletList(ref List<Bullet> bulletList)
{
bullets = bulletList;
}
public Player(SpriteBatch spritebatch, Game game)
: base(spritebatch, game)
{
}
public override void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Up))
{
speed += acceleration;
if (speed > maxSpeed)
{
speed = maxSpeed;
}
}
if (keyState.IsKeyDown(Keys.Down))
{
speed -= acceleration;
if (speed < 0.0f)
{
speed = 0.0f;
}
}
if (keyState.IsKeyDown(Keys.Left))
{
playerRotation -= rotationSpeed * (float)gameTime.ElapsedGameTime.Milliseconds;
}
if (keyState.IsKeyDown(Keys.Right))
{
playerRotation += rotationSpeed * (float)gameTime.ElapsedGameTime.Milliseconds;
}
// Get the current mouse position
mousePosition.X = Mouse.GetState().X;
mousePosition.Y = Mouse.GetState().Y;
// Get The direction from the player position to the mouse position
Vector2 directionToMouse = mousePosition - position;
// Get the angle of the directional vector (Note that it must be a unit vector so we have to normalize it)
directionToMouse.Normalize();
ringRotation = (float)Math.Atan2((float)directionToMouse.Y, (float)directionToMouse.X);
velocity = Vector2.Zero;
velocity.X += speed * (float)Math.Cos(playerRotation);
velocity.Y += speed * (float)Math.Sin(playerRotation);
position += velocity * (float)gameTime.ElapsedGameTime.Milliseconds;
shootTimeLeft -= gameTime.ElapsedGameTime.Milliseconds;
if (shootTimeLeft <= 0 && keyState.IsKeyDown(Keys.Space))
{
Bullet bullet = new Bullet(spriteBatch, Game);
bullet.Initialize();
bullet.velocity.X = (float)Math.Cos(playerRotation);
bullet.velocity.Y = (float)Math.Sin(playerRotation);
bullet.position = position;
bullet.playerRotation = playerRotation;
bullet.speed += speed;
bullet.screenBoundaries = screenBoundaries;
bullets.Add(bullet);
shootTimeLeft = shootTime;
}
base.Update(gameTime);
}
public override void Initialize()
{
screenBoundaries = new Rectangle(0, 0,
Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height);
position = new Vector2(screenBoundaries.Width * 0.5f, screenBoundaries.Height * 0.5f);
ringPosition = position; //new Vector2(screenBoundaries.Width * 0.5f, screenBoundaries.Height * 0.5f);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// Load our player sprite
texture = Game.Content.Load<Texture2D>("Player");
ringTexture = Game.Content.Load<Texture2D>("ring");
// Set the rotation point to be in the center of our character
playerRotationPoint = new Vector2(texture.Width / 2, texture.Height / 2);
ringRotationPoint = new Vector2(ringTexture.Width / 2, ringTexture.Height / 2);
// Set the default rotation in radians (facing to the right)
playerRotation = 0.0f;
ringRotation = 0.0f;
// Just set the mouse position to a default value here (we will update it
// in the Update(GameTime)-method)
mousePosition = Vector2.Zero;
}
}
}
Bullet.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace RotateTowardsMouse
{
class Bullet : Entity
{
public Bullet(SpriteBatch spritebatch, Game game)
: base(spritebatch, game)
{
}
public override void Initialize()
{
speed = 0.8f;
base.Initialize();
}
protected override void LoadContent()
{
texture = Game.Content.Load<Texture2D>("bullet");
origin = new Vector2(0.0f, (float)texture.Height * 0.5f);
screenBoundaries = new Rectangle(0, 0,
Game.Window.ClientBounds.Width,
Game.Window.ClientBounds.Height);
base.LoadContent();
}
public override void Update(GameTime gameTime)
{
if(position.X - texture.Width > screenBoundaries.Width)
{
killMe = true;
}
else if (position.X + texture.Width < screenBoundaries.X)
{
killMe = true;
}
if (position.Y - texture.Height > screenBoundaries.Height)
{
killMe = true;
}
else if (position.Y + texture.Height < screenBoundaries.Y)
{
killMe = true;
}
position += velocity * speed * (float)gameTime.ElapsedGameTime.Milliseconds;
}
/*public override void OnCollision()
{
killMe = true;
}
*/
}
}