XNA Bullet Hitbox wont follow the sprite thus causing problems with collision - c#

Im making a spaceinvaderisch kinda game in XNA/Monogame
Everything was going great until I encountered this problem.
Ive been trying to fix the bullet hitboxes for days but to no succes.
My problem lies in the fact that the hitbox doesnt move with the bullet itself.
The Collision detection is in the Game1 class. The actual movement in Bullets class and the trigger to shoot is in the Tank class. The Bullet is also inherited to Sprite.cs which gives it a hitbox.
Could anyone help me out. I really just want to finish what ive started before I start working on something else.
Help is Dearly Appreciated.
Game1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace IsisInvaders
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Tank Tank;
Isis Isis1;
Isis Isis2;
Isis Isis3;
Isis Isis4;
Isis Isis5;
Isis Isis6;
Isis Isis7;
Isis Isis8;
Isis Isis9;
Isis Isis10;
Isis Isis11;
Bullets Bullets;
private Song BgMusic;
int screenWidth;
int screenHeight;
enum GameState
{
MainMenu,
Playing,
}
GameState CurrentGameState = GameState.MainMenu;
Button btnPlay;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <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
//Bullet = new Bullets(Tank.position);
Tank = new Tank(new Vector2 (375,425));
Isis1 = new Isis(new Vector2(30, 0));
Isis2 = new Isis(new Vector2(70, 0));
Isis3 = new Isis(new Vector2(110, 0));
Isis4 = new Isis(new Vector2(150, 0));
Isis5 = new Isis(new Vector2(190, 0));
Isis6 = new Isis(new Vector2(230, 0));
Isis7 = new Isis(new Vector2(270, 0));
Isis8 = new Isis(new Vector2(310, 0));
Isis9 = new Isis(new Vector2(350, 0));
Isis10 = new Isis(new Vector2(390, 0));
Isis11 = new Isis(new Vector2(430, 0));
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()
{
graphics.PreferredBackBufferHeight = 800;
graphics.PreferredBackBufferHeight = 600;
graphics.ApplyChanges();
btnPlay = new Button(Content.Load<Texture2D>("Start"), graphics.GraphicsDevice);
IsMouseVisible = true;
spriteBatch = new SpriteBatch(GraphicsDevice);
btnPlay.setPosition(new Vector2(350, 300));
BgMusic = Content.Load<Song>("Music");
Tank.LoadContent(this.Content, "Tank");
Isis1.LoadContent(this.Content, "Isis");
Isis2.LoadContent(this.Content, "Isis");
Isis3.LoadContent(this.Content, "Isis");
Isis4.LoadContent(this.Content, "Isis");
Isis5.LoadContent(this.Content, "Isis");
Isis6.LoadContent(this.Content, "Isis");
Isis7.LoadContent(this.Content, "Isis");
Isis8.LoadContent(this.Content, "Isis");
Isis9.LoadContent(this.Content, "Isis");
Isis10.LoadContent(this.Content, "Isis");
Isis11.LoadContent(this.Content, "Isis");
Sounds.BgMusic(BgMusic);
screenWidth = GraphicsDevice.Viewport.Width;
screenHeight = GraphicsDevice.Viewport.Height;
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific 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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
//Bullet.Update(screenWidth, screenHeight, gameTime);
MouseState mouse = Mouse.GetState();
switch (CurrentGameState)
{
case GameState.MainMenu:
if (btnPlay.isClicked == true)
{
CurrentGameState = GameState.Playing;
}
btnPlay.Update(mouse);
break;
case GameState.Playing:
Isis1.Update(screenWidth, screenHeight, gameTime);
Isis2.Update(screenWidth, screenHeight, gameTime);
Isis3.Update(screenWidth, screenHeight, gameTime);
Isis4.Update(screenWidth, screenHeight, gameTime);
Isis5.Update(screenWidth, screenHeight, gameTime);
Isis6.Update(screenWidth, screenHeight, gameTime);
Isis7.Update(screenWidth, screenHeight, gameTime);
Isis8.Update(screenWidth, screenHeight, gameTime);
Isis9.Update(screenWidth, screenHeight, gameTime);
Isis10.Update(screenWidth, screenHeight, gameTime);
Isis11.Update(screenWidth, screenHeight, gameTime);
Tank.Update(screenWidth, screenHeight, gameTime);
break;
}
//Collision
foreach (Bullets item in Tank.bullets)
{
if (item.Bullethitbox.Intersects(Isis1.Box))
{
Isis1.LoadContent(this.Content, "DeadIsis");
}
}
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.SandyBrown);
spriteBatch.Begin();
switch (CurrentGameState)
{
case GameState.MainMenu:
spriteBatch.Draw(Content.Load<Texture2D>("MainMenu"), new Rectangle(0, 0, 800, 600), Color.White);
btnPlay.Draw(spriteBatch);
break;
case GameState.Playing:
Tank.Draw(spriteBatch);
Isis1.Draw(spriteBatch);
Isis2.Draw(spriteBatch);
Isis3.Draw(spriteBatch);
Isis4.Draw(spriteBatch);
Isis5.Draw(spriteBatch);
Isis6.Draw(spriteBatch);
Isis7.Draw(spriteBatch);
Isis8.Draw(spriteBatch);
Isis9.Draw(spriteBatch);
Isis10.Draw(spriteBatch);
Isis11.Draw(spriteBatch);
break;
}
//Bullet.Draw(spriteBatch);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
Bullets.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace IsisInvaders
{
class Bullets : Sprite
{
private Vector2 Position;
public Bullets(Vector2 Position) : base(Position)
{
this.Position = Position;
}
public override void Update(int screenWidth, int Screenheight, GameTime gametime)
{
int Pos = (int)Position.Y;
Pos -= 10;
Position.Y = Pos;
}
public override void LoadContent(ContentManager Contentman, string Spritename)
{
base.LoadContent(Contentman, Spritename);
spritetext = Contentman.Load<Texture2D>("Bullet");
//this.Box = new Rectangle((int)Position.X, (int)Position.Y, spritetext.Width, spritetext.Height);
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(spritetext, Position, Color.White);
}
public Texture2D Bulletsprite { get { return this.spritetext; } }
public Vector2 Bulletposition { get { return this.Position; } set { this.Position = value; } }
public Rectangle Bullethitbox { get { return this.Box; } set { this.Box = value; } }
}
}
Tank.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 IsisInvaders
{
class Tank : Sprite
{
private Vector2 P;
private KeyboardState KBS = Keyboard.GetState();
private float bulletspeed = -400f;
public List<Bullets> bullets = new List<Bullets>();
private float reload = 0;
Bullets Bullet;
ContentManager Content;
public Tank(Vector2 Position) : base(Position)
{
}
public override void Update(int screenWidth, int Screenheight, GameTime gametime)
{
KBS = Keyboard.GetState();
P = this.position;
if (KBS.IsKeyDown(Keys.Right))
{
P.X += 8;
this.position = P;
}
if (KBS.IsKeyDown(Keys.Left))
{
P.X -= 8;
this.position = P;
}
reload += (float)gametime.ElapsedGameTime.TotalMilliseconds;
if (reload >= 500 && KBS.IsKeyDown(Keys.Space))
{
Bullets b = new Bullets(position);
b.LoadContent(Content, "Bullet");
bullets.Add(b);
reload = 0;
}
foreach (Bullets item in bullets)
{
//item.Bullethitbox = new Rectangle((int)item.Bulletposition.X, (int)item.Bulletposition.Y, item.Bulletsprite.Width, item.Bulletsprite.Height);
item.Update(screenWidth, Screenheight, gametime);
}
//for (int i = 0; i < bullets.Count; i++)
//{
// float y = bullets[i].position.Y;
// y += bulletspeed * (float)gametime.ElapsedGameTime.TotalSeconds;
// bullets[i] = new Bullets(new Vector2(bullets[i].position.X, y));
//}
}
public override void LoadContent(ContentManager Contentman, string Spritename)
{
base.LoadContent(Contentman, Spritename);
this.Content = Contentman;
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(spritetext, position, Color.White);
foreach (Bullets b in bullets)
{
b.Draw(spriteBatch);
}
//for (int i = 0; i < bullets.Count; i++)
//{
// spriteBatch.Draw(Bullet.Bulletsprite, position, Color.White);
//}
}
}
}
Isis.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 IsisInvaders
{
class Isis : Sprite
{
private Vector2 P;
public Isis(Vector2 Position) : base (Position)
{
P = Position;
}
public override void Update(int screenWidth, int screenHeight, GameTime gametime)
{
//P.Y += (float)0.11;
this.position = P;
this.Box = new Rectangle((int)P.X, (int)P.Y, spritetext.Width, spritetext.Height);
}
}
}
Sprite.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace IsisInvaders
{
class Sprite
{
private Texture2D SpriteTexture;
private Rectangle Hitbox;
private Vector2 Position;
public Sprite(Vector2 Position)
{
this.Position = Position;
}
public virtual void Update(int screenWidth, int Screenheight, GameTime gametime)
{
}
public virtual void LoadContent(ContentManager Contentman, string Spritename)
{
SpriteTexture = (Contentman.Load<Texture2D>(Spritename));
}
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(SpriteTexture, Position, Color.White);
}
public Texture2D spritetext { get { return this.SpriteTexture; } set { this.SpriteTexture = value; } }
public Vector2 position { get { return this.Position; } set { this.Position = value; } }
public Rectangle Box { get { return new Rectangle((int)Position.X, (int)Position.Y, SpriteTexture.Width, SpriteTexture.Height); } set { this.Hitbox = value; } }
}
}

In your Bullets class you got a local variable called Position which will be modified in Bullets.Update(GameTime) but your hitbox refers to the Position vector in your Sprite class. Hope it helps.

Related

(monogame) Multiple instances of sprite inherited object

I've seen questions similar to what I'm asking, but they don't quite give me the answer I'm looking for. I want 5 instances of my sprites to show up on screen and spawn in different locations. For some reason though instead of 5 instances, each "instance" of the sprite increases the values of the one sprite that does appear on screen.
Sprite.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;
namespace Lab05_2__Aaron_Benjamin_
{
class Sprite
{
Texture2D mSpriteTexture;
public Vector2 Position;
Color mSpriteColor;
public Rectangle Size;
public string AssetName;
private float mScale = 1.0f;
public float Scale
{
get { return mScale; }
set
{
mScale = value;
//Recalculate the Size of the Sprite with the new scale
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
}
}
public void LoadContent(ContentManager theContentManager, string theAssetName)
{
mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
AssetName = theAssetName;
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
}
public void Update(GameTime gameTime)
{
}
public void Draw(SpriteBatch theSpriteBatch)
{
theSpriteBatch.Draw(mSpriteTexture, Position,
new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
//theSpriteBatch.Draw(mSpriteTexture, Position);
}
}
}
Enemy.cs
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace Lab05_2__Aaron_Benjamin_
{
class Enemy : Sprite
{
const string ENEMY_ASSETNAME = "gear";
Random Rand = new Random();
int startPos_X;
int startPos_Y;
public void LoadContent(ContentManager theContentManager)
{
Position = new Vector2(startPos_X = Rand.Next(0 , 1000), startPos_Y = Rand.Next(0, 1000)); //= Rand.Next(0, 100),Position.Y = Rand.Next(0,100));
base.LoadContent(theContentManager, ENEMY_ASSETNAME);
}
public void Update(GameTime gameTime)
{
MouseState cState = Mouse.GetState();
if (Position.X > cState.X)
{
Position.X += 1;
}
if (Position.X < cState.X)
{
Position.X -= 1;
}
if (Position.Y < cState.Y)
{
Position.Y -= 1;
}
if (Position.Y > cState.Y)
{
Position.Y += 1;
}
base.Update(gameTime);
}
}
}
Game1.cs
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Lab05_2__Aaron_Benjamin_
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
List<Enemy> enemies = new List<Enemy>();
private void CreateEnemy()
{
Enemy gear = new Enemy();
Random rand = new Random();
//gear = new Enemy();
//gear.Position.X = rand.Next(0, 1000);
//gear.Position.Y = rand.Next(0, 1000);
Console.WriteLine(gear.Position.Y);
enemies.Add(gear);
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
/// <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);
// TODO: use this.Content to load your game content here
for (int i = 0; i < 5; i++)
{
CreateEnemy();
}
foreach (var cog in enemies)
{
cog.LoadContent(this.Content);
}
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific 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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
foreach (var cog in enemies)
{
if (cog.Position.X > Window.ClientBounds.Width - 50)
cog.Position.X = Window.ClientBounds.Width - 50;
if (cog.Position.Y > Window.ClientBounds.Height - 50)
cog.Position.Y = Window.ClientBounds.Height - 50;
if (cog.Position.X < 0)
cog.Position.X = 0;
if (cog.Position.Y < 0)
cog.Position.Y = 0;
}
foreach (var cog in enemies)
{
cog.Update(gameTime);
}
if (Keyboard.GetState().IsKeyDown(Keys.M))
{
// If 'm' is down, we create a new meteor. Note that once this is working
// this is going to make a lot of meteors. That's another issue, though.
CreateEnemy();
}
//Console.WriteLine(enemies.Count);
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.Black);
// TODO: Add your drawing code here
spriteBatch.Begin();
foreach (var cog in enemies)
{
cog.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Shouldn't you be using gear in this part instead of this.gear ?
foreach (var gear in enemies)
{
this.gear.LoadContent(this.Content);
}
ok I got it to work. So my randoms were being assigned and then reassigned in a different section of the code so that they all stacked on top of each other. Here is the solution.
Sprite.cs
class Sprite
{
Texture2D mSpriteTexture;
public Vector2 Position;
Color mSpriteColor;
public Rectangle Size;
public string AssetName;
private float mScale = 1.0f;
public float Scale
{
get { return mScale; }
set
{
mScale = value;
//Recalculate the Size of the Sprite with the new scale
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
}
}
public void LoadContent(ContentManager theContentManager, string theAssetName)
{
mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
AssetName = theAssetName;
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
}
public void Update(GameTime gameTime)
{
}
public void Draw(SpriteBatch theSpriteBatch)
{
theSpriteBatch.Draw(mSpriteTexture, Position,
new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height),
Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
//theSpriteBatch.Draw(mSpriteTexture, Position);
}
}
}
Enemy.cs
class Enemy : Sprite
{
const string ENEMY_ASSETNAME = "gear";
Random Rand = new Random();
//int startPos_X;
//int startPos_Y;
public void LoadContent(ContentManager theContentManager)
{
Position = new Vector2(Position.X ,Position.Y);
base.LoadContent(theContentManager, ENEMY_ASSETNAME);
}
public void Update(GameTime gameTime)
{
MouseState cState = Mouse.GetState();
if (Position.X > cState.X)
{
Position.X += 1;
}
if (Position.X < cState.X)
{
Position.X -= 1;
}
if (Position.Y < cState.Y)
{
Position.Y -= 1;
}
if (Position.Y > cState.Y)
{
Position.Y += 1;
}
base.Update(gameTime);
}
}
}
Game1.cs
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
List<Enemy> enemies = new List<Enemy>();
Random rand = new Random();
private void CreateEnemy()
{
Enemy gear = new Enemy();
//gear = new Enemy();
gear.Position.X = rand.Next(0, 1000);
gear.Position.Y = rand.Next(0, 1000);
Console.WriteLine(gear.Position.Y);
enemies.Add(gear);
enemies[0].Position.X += 10;
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
/// <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);
// TODO: use this.Content to load your game content here
for (int i = 0; i < 5; i++)
{
CreateEnemy();
}
foreach (var cog in enemies)
{
cog.LoadContent(this.Content);
}
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific 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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
foreach (var cog in enemies)
{
if (cog.Position.X > Window.ClientBounds.Width - 50)
cog.Position.X = Window.ClientBounds.Width - 50;
if (cog.Position.Y > Window.ClientBounds.Height - 50)
cog.Position.Y = Window.ClientBounds.Height - 50;
if (cog.Position.X < 0)
cog.Position.X = 0;
if (cog.Position.Y < 0)
cog.Position.Y = 0;
}
foreach (var cog in enemies)
{
cog.Update(gameTime);
}
if (Keyboard.GetState().IsKeyDown(Keys.M))
{
// If 'm' is down, we create a new meteor. Note that once this is working
// this is going to make a lot of meteors. That's another issue, though.
CreateEnemy();
}
//Console.WriteLine(enemies.Count);
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.Black);
// TODO: Add your drawing code here
spriteBatch.Begin();
foreach (var cog in enemies)
{
cog.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}

How to make a sprite change to another sprite from the sprite sheet?

i am working on a game, and i want the speceship to change to another spaceship from the sprite sheet when the player presses Tab key. so its like switching between the spaceships.
i have been trying with GetSourceRectangle from the class and setting one, and updating that in game, but its not working.
here is the code fro the Spaceship class:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SpaceShooterTest1
{
class Spaceship
{
public int xPos = 0;
public int yPos = 0;
private int width = 128;
private int height = 128;
private Texture2D texture;
public Rectangle currentSourceRect { get; set; } //determines which part of sprite sheet to show
public Spaceship(Texture2D tex)
{
texture = tex;
currentSourceRect = new Rectangle(0, 0, 128, 128);
}//end Spaceship
// // //
public void MoveToPosition(int x, int y)
{
xPos = x;
yPos = y;
}//end MoveToPosition
public void Update(GameTime gametime)
{
currentSourceRect = GetSourceRectangle(2, 0); // this could be getting the fiery weapons
//currentSourceRect = SetSourceRectangle(2, 0);
}
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, new Rectangle(xPos, yPos, width, height), currentSourceRect, Color.White);
}//end Draw
public Rectangle GetSourceRectangle(int row, int col)
{
Rectangle r;
//TODO: Make custom based on row and col
r = new Rectangle(0, 128, width, height);
return r;
}//end GetSourseRectangle
//public Rectangle SetSourceRectangle(int row, int col)
//{
// Rectangle r;
// //TODO: Make custom based on row and col
// r = new Rectangle(0, 128, width, height);
// return r;
//}//end GetSourseRectangle
}
}
and here is the code for the 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 SpaceShooterTest1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
//Game State Enum
enum GameState { GScreen, Playing, Won, Lost };
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Random rand;
int playerScore = 0;
//Textures
Texture2D galaxyScreen;
Texture2D texShip;
GameState currentState = GameState.Playing;
//GameState currentState = GameState.GScreen; /// use after
//ship
Spaceship spaceShip;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <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();
rand = new Random();
}
/// <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);
texShip = Content.Load<Texture2D>(#"Images\ships_sm");
spaceShip = new Spaceship(texShip);
spaceShip.xPos = 0;
spaceShip.yPos = Window.ClientBounds.Height - 128;
//galaxyScreen = Content.Load<Texture2D>(#"Images\galaxy");
// TODO: use this.Content to load your game content here
}
/// <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();
KeyboardState keyboardState = Keyboard.GetState();
if (Keyboard.GetState().IsKeyDown(Keys.Tab))
{
spaceShip.GetSourceRectangle(2, 0);
}
if (Keyboard.GetState().IsKeyDown(Keys.D))
{
spaceShip.xPos += 5;
}
else if (Keyboard.GetState().IsKeyDown(Keys.A))
{
spaceShip.xPos -= 5;
}
if (spaceShip.xPos < 0)
spaceShip.xPos = 0;
if (spaceShip.xPos + 128 > Window.ClientBounds.Width)
{
spaceShip.xPos = Window.ClientBounds.Width - 128;
}
/*if (currentState == GameState.GScreen && keyboardState.IsKeyDown(Keys.Space))
{
currentState = GameState.Playing;
}
spaceShip.Update(gameTime);
*/
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);
//draw ship
spriteBatch.Begin();
if (currentState == GameState.Playing)
{
spaceShip.Draw(gameTime, spriteBatch);
}
//800 wide x 400 high
else if (currentState == GameState.GScreen)
{
spriteBatch.Draw(galaxyScreen, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
apparently i cant post the spritesheet because i dont have enough reputation (seriously stack?) but i hope you guys understand what i mean.
With what you gave, what I can think of is:
Have 2 Vector2 (or Rectangle, whichever object you use on your Draw method) objects on the Spaceship class, one for the coordinates of spaceship when the Tab is not pressed, another one for when the key is pressed on the tilesheet.
Then you can have a bool value on the Game class to specify which texture to use.
On the Update method, update the bool value.
On the Draw method, draw the texture according the bool value.
So basically on the Spaceship class:
Texture2D spaceship;
Vector2 spaceship1, spaceship2;
bool tabPressed;
On the Update method of the Game class:
tabPressed = Keyboard.GetState().IsKeyDown(Keys.Tab);
Now you can either pass the bool value to your Spaceship Draw method and draw accordingly, or access a property/method of the class to signal the change in the desired drawing behavior.

How do I switch between animations in my C# XNA program?

I'm trying to animate a character and make him walk left and right. While I've learned how to make a basic animation, I can't figure out how to switch between them.
When the character walks left I'd like him to switch from the 'idle' (animatedSprite) animation to the 'walking left'(animatedSprite2) animation.
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 WalkingAnimation
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private AnimatedSprite animatedSprite, animatedSprite2;
private Vector2 position = new Vector2(350f, 200f);
private KeyboardState keyState;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Texture2D texture = Content.Load<Texture2D>("Idle");
Texture2D texture2 = Content.Load<Texture2D>("Run");
animatedSprite = new AnimatedSprite(texture, 1, 11);
animatedSprite2 = new AnimatedSprite(texture2, 1, 10);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Q))
{
position.X -= 3;
}
if (keyState.IsKeyDown(Keys.P))
{
position.X += 3;
}
base.Update(gameTime);
animatedSprite.Update();
animatedSprite2.Update();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
animatedSprite.Draw(spriteBatch, position);
}
}
}
Like you so clearly expressed in your question, all you need to do is store a Boolean which describes whether the character is idle or not:
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 WalkingAnimation
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private AnimatedSprite animatedSprite, animatedSprite2;
private Vector2 position = new Vector2(350f, 200f);
private KeyboardState keyState;
private bool idle;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
// Start of as idle
idle = true;
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// Textures
Texture2D texture = Content.Load<Texture2D>("Idle");
Texture2D texture2 = Content.Load<Texture2D>("Run");
// Animations
animatedSprite = new AnimatedSprite(texture, 1, 11);
animatedSprite2 = new AnimatedSprite(texture2, 1, 10);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
keyState = Keyboard.GetState();
idle = true; // If the character doesn't move this will stay true
if (keyState.IsKeyDown(Keys.Q))
{
position.X -= 3;
idle = false;
}
if (keyState.IsKeyDown(Keys.P))
{
position.X += 3;
idle = false;
}
base.Update(gameTime);
animatedSprite.Update();
animatedSprite2.Update();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
if(idle)
animatedSprite.Draw(spriteBatch, position);
else
animatedSprite2.Draw(spriteBatch, position);
}
}
}
And that should do what you want.
Also, if animations get a bit clunky I would recommend using sprite sheets and creating your own Animation class which can store all the player variables. This is extra-useful if you decide to make your game multiplayer.
Lets say left and right walking anmation uses 5 frame. And it's on single texture.
Function Update
if State = Left
currentFrame +=1;
if currentFrame > 5 then currentFrame = 0
texureSource = new rectangle(32*currentFrame,0,32,23);
end if
if State = Right
currentFrame +=1;
if currentFrame > 5 then currentFrame = 0
texureSource = new rectangle(32*currentFrame,32,32,23);
end if
End Function

How to prevent player object from clipping through walls in a 2d environment

The player box is continuing through walls in an undesired fashion, I have tried making it so that the player moves in 0.1f(u) increments at a time, but this severely drops the performance of the game. Is there any way I can detect if the player is hitting a wall, what side they hit it on and how can I prevent them from clipping into the wall?
Here is the code that I am running (this is minimalistic of course)
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 Platformer
{
public class Player
{
double terminalVelocity;
//AnimatedTexture texture;
public Texture2D texture;
public Vector2 Position, Velocity;
public Rectangle boundingBox;
public Player(Texture2D tex, Vector2 pos, Vector2 vel, Rectangle bb)
{
texture = tex;
Position = pos;
Velocity = vel;
boundingBox = bb;
terminalVelocity = Math.Sqrt((2*bb.Width*bb.Height*Game1.gravity)/-9.8*2);
}
public void updateBoundingBoxes()
{
boundingBox.X = (int)Position.X;
boundingBox.Y = (int)Position.Y;
}
public void onUpdate()
{
updateBoundingBoxes();
Position.X += Velocity.X;
Position.Y += Velocity.Y;
//Velocity = Vector2.Zero;
Velocity.Y += Game1.gravity / 60;
Velocity.X /= 1.2f;
}
public void Draw(SpriteBatch sb)
{
updateBoundingBoxes();
sb.Begin();
sb.Draw(texture,boundingBox,GameLighting.currentColour());
sb.End();
}
}
public enum GameLightingState
{
Red, Dark, Orange, Blue, White
}
public class Platform : Object
{
Texture2D text;
public Rectangle rect;
public Platform(Texture2D t, Vector2 p, int sizeX, int sizeY)
{
text = t;
rect = new Rectangle((int)p.X, (int)p.Y, sizeX, sizeY);
}
public void onPlayerCollision(Player p)
{
p.Velocity.X = -p.Velocity.X / 2;
p.Velocity.Y = -p.Velocity.Y / 2;
}
public void Draw(SpriteBatch sb)
{
sb.Begin();
sb.Draw(text, rect, GameLighting.currentColour());
sb.End();
}
public void onUpdate()
{
}
}
public class GameLighting
{
public static Color currentColour()
{
return eToColour(Game1.currentLightingState);
}
public static Color eToColour(GameLightingState gls)
{
switch (gls)
{
case(GameLightingState.Red):
return Color.Red;
case (GameLightingState.Blue):
return Color.Blue;
case (GameLightingState.Orange):
return Color.Orange;
case (GameLightingState.Dark):
return Color.DarkGray;
case (GameLightingState.White):
return Color.White;
}
return Color.White;
}
}
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
public static float gravity = 9.80665f;
public static GameLightingState currentLightingState;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
List<Platform> platforms;
List<Player> players;
int controlledPlayerIndex = 0;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <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
currentLightingState = GameLightingState.White;
platforms = new List<Platform>();
players = new List<Player>();
players.Add(new Player(this.Content.Load<Texture2D>("Images/dirt"), new Vector2(300,0), new Vector2(0,0), new Rectangle(300,0,20,20)));
platforms.Add(new Platform(this.Content.Load<Texture2D>("Images/dirt"),new Vector2(300,450),200,20));
platforms.Add(new Platform(this.Content.Load<Texture2D>("Images/dirt"), new Vector2(20,20), 20, 200));
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);
// TODO: use this.Content to load your game content here
}
/// <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();
foreach (Player p in players)
{
Boolean intersects = false;
Rectangle tempRectangle = new Rectangle((int)(p.Position.X + p.Velocity.X),(int) (p.Position.Y + p.Velocity.Y), p.boundingBox.Width, p.boundingBox.Height);
foreach (Platform pl in platforms)
{
intersects = intersects || tempRectangle.Intersects(pl.rect);
}
if (!intersects)
{
p.onUpdate();
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
players[controlledPlayerIndex].Velocity.Y -= 0.75f;
}
if (Keyboard.GetState().IsKeyDown(Keys.A))
{
players[controlledPlayerIndex].Velocity.X -= 0.75f;
}
if (Keyboard.GetState().IsKeyDown(Keys.D))
{
players[controlledPlayerIndex].Velocity.X += 0.75f;
}
// 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
foreach (Platform p in platforms)
{
p.Draw(spriteBatch);
}
foreach (Player p in players)
{
p.Draw(spriteBatch);
}
base.Draw(gameTime);
}
}
}
*Updated Source Code based on first comments
One note about this code, you need to run it in XNA and use an icon called dirt.png in a folder called Images, it doesn't matter what the picture looks like, you just need it to fully understand what is happening
Had a similar problem recently myself. Here is what I did.
//variables needed
public bool follow = true;
public Vector2D startpoint;
//puts the startpoint value equal
//to the inital location of the player
public Player()
{
startpoint.X = rectangle.X;
startpoint.Y = rectangle.Y;
}
//if any of the collision tests fail
if(collision occurs)
{
collision();
}
//else update startpoint to new valid location
else
{
startpoint.X = rectangle.X;
startpoint.Y = rectangle.Y;
follow = true;
}
if(follow == true)
{
//movement commands occur
}
else
{
follow = true;
}
//if a condition fails set player
//too last valid location
public void collision()
{
rectangle.X = startpoint.X;
rectangle.Y = startpoint.Y;
follow = false;
}
This worked well enough for me hope it helps.

Why won't my sprite draw? (XNA)

I was just messing around with XNA and trying to get a simple player class that could move around through keyboard controls. I can't seem to get it to draw the way I have it set up now though. I'm not seeing what error I'm making.
Here's the Actor base class:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace RunnerTEst
{
class Actor
{
public static List<Actor> actors;
public Texture2D texture;
protected Vector2 position;
protected float rotation;
protected float scale;
protected Color color;
#region Constructors
static Actor()
{
actors = new List<Actor>();
}
public Actor(Texture2D texture)
{
actors.Add(this);
this.texture = texture;
this.position = Vector2.Zero;
this.rotation = 0f;
this.scale = 1f;
}
#endregion
#region Properties
public Vector2 Position
{
get { return this.position; }
set { this.position = value; }
}
public Vector2 Origin
{
get { return new Vector2(this.position.X + this.texture.Width / 2,
this.position.Y + this.texture.Height / 2); }
}
public float Rotation
{
get { return this.rotation; }
set { this.rotation = value; }
}
public float Scale
{
get { return this.scale; }
set { this.scale = value; }
}
public Color Color
{
get { return this.color; }
set { this.color = value; }
}
public float Width
{
get { return this.texture.Width; }
}
public float Height
{
get { return this.texture.Height; }
}
#endregion
#region Methods
public virtual void Update(GameTime gameTime)
{
}
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(this.texture, this.position, this.color);
}
#endregion
}
}
Player class:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace RunnerTEst
{
class Player : Actor
{
private const float speed = 5f;
protected Vector2 velocity;
private static KeyboardState currState, prevState;
static Player()
{
currState = prevState = Keyboard.GetState();
}
public Player(Texture2D texture, Vector2 position)
: base(texture)
{
this.position = position;
this.velocity = Vector2.Zero;
}
public override void Update(GameTime gameTime)
{
this.HandleInput();
this.position += this.velocity;
foreach (Actor actor in Actor.actors)
{
if (this == actor)
continue;
this.CheckCollision(actor);
}
base.Update(gameTime);
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);
}
public void CheckCollision(Actor actor)
{
//--left/right sides
if (this.position.X + this.Width > actor.Position.X) //right of this hitting left actor
{
this.position.X = actor.Position.X - this.Width;
}
else if (this.position.X < (actor.Position.X + actor.Width))//left this hit right actor
{
this.position.X = actor.Position.X + actor.Width;
}
//--top/bottom
if (this.position.Y + this.Height > actor.Position.Y) //this bottom hit actor top
{
this.position.Y = actor.Position.Y - this.Width;
}
else if (this.position.Y < (actor.Position.Y + actor.Height))//this top hit actor bottom
{
this.position.Y = actor.Position.Y + actor.Height;
}
//TODO: check screen bounds
}
public void HandleInput()
{
currState = Keyboard.GetState();
if (currState.IsKeyDown(Keys.W))
{
this.velocity.Y = -speed;
}
else if (currState.IsKeyDown(Keys.S))
{
this.velocity.Y = speed;
}
else
{
this.velocity.Y = 0f;
}
if (currState.IsKeyDown(Keys.A))
{
this.velocity.X = -speed;
}
else if (currState.IsKeyDown(Keys.D))
{
this.velocity.X = speed;
}
else
{
this.velocity.X = 0f;
}
prevState = currState;
}
}
}
and lastly, the 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 RunnerTEst
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D playerTexture;
Player me;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
playerTexture = Content.Load<Texture2D>(#"Textures\player");
me = new Player(playerTexture, new Vector2(200, 200));
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
me.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
me.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Thanks in advance for your time!
I looked through it, and it looks like you never define the Actor's color property. This means that the color variable defaults to black, which means that the tint given to the Draw line (spriteBatch.Draw(this.texture, this.position, this.color);) is Black, which hides the sprite.
Just set the color of the Actor to white somewhere (I just set it underneath me = new Player(playerTexture, new Vector2(200, 200));).
So the new LoadContent would look like:
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
playerTexture = Content.Load<Texture2D>(#"Textures\player");
me = new Player(playerTexture, new Vector2(200, 200));
me.Color = Color.White;
}
Hope that helps,
max

Categories

Resources