V
Visual Studio FAN
Guest
Well I already have my player moving around, ducking, jumping, shoting fireballs off his hands, BUT for all these the project uses 2 of 8 sprites in my spritesheet! I also have 2 images in my fireball spritesheet and the game uses the whole image and not swap first and second frame. So I wonder how to make it use different images of the stylesheet when a key is pressed (and player state has begun)?
My Sprite.cs code:
#region Sprite.cs Code
#region Using Statements
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.Audio;
#endregion
#region Sprite Logic
namespace My project
{
class Sprite
{
//The Rectangular area from the original image that defines the Sprite.
Rectangle mSource;
public Rectangle Source
{
get { return mSource; }
set
{
mSource = value;
Size = new Rectangle(0, 0, (int)(mSource.Width * Scale), (int)(mSource.Height * Scale));
}
}
//The current position of the Sprite
public Vector2 Position = new Vector2(0, 0);
//The texture object used when drawing the sprite
private Texture2D mSpriteTexture;
//The asset name for the Sprites Texture
public string AssetName;
//The Size of the Sprite (with scale applied)
public Rectangle Size;
//The amount to increase/decrease the size of the original sprite. When
//modified throught he property, the Size of the sprite is recalculated
//with the new scale applied.
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)(Source.Width * Scale), (int)(Source.Height * Scale));
}
}
//Load the texture for the sprite using the Content Pipeline
public void LoadContent(ContentManager theContentManager, string theAssetName)
{
mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
AssetName = theAssetName;
Source = new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height);
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
//Recalculate the Size of the Sprite with the new scale
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
}
//Update the Sprite and change its position based on the passed in speed, direction and elapsed time.
public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection)
{
Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
}
//Draw the sprite to the screen
public virtual void Draw(SpriteBatch theSpriteBatch)
{
theSpriteBatch.Draw(mSpriteTexture, Position, Source,
Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
}
}
}
#endregion
#endregion
And this is my Player.cs code:
#region Player Code
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Microsoft.Xna.Framework.Audio;
#endregion
#region Player Logic
namespace My Project
{
class Player : Sprite
{
List<Fireball> mFireballs = new List<Fireball>();
ContentManager mContentManager;
Vector2 mStartingPosition = Vector2.Zero;
enum State
{
Walking,
Jumping,
Ducking
}
State mCurrentState = State.Walking;
Vector2 mDirection = Vector2.Zero;
Vector2 mSpeed = Vector2.Zero;
KeyboardState mPreviousKeyboardState;
const string PLAYER_ASSETNAME = "player-3";
const int START_POSITION_X = 10;
const int START_POSITION_Y = 350;
const int PLAYER_SPEED = 160;
const int MOVE_UP = -1;
const int MOVE_DOWN = 1;
const int MOVE_LEFT = -1;
const int MOVE_RIGHT = 1;
public void LoadContent(ContentManager theContentManager)
{
mContentManager = theContentManager;
foreach (Fireball aFireball in mFireballs)
{
aFireball.LoadContent(theContentManager);
}
Position = new Vector2(START_POSITION_X, START_POSITION_Y);
base.LoadContent(theContentManager, PLAYER_ASSETNAME);
Source = new Rectangle(0, 0, 28, 56); //We cant use Source.Height instead "56" here...
}
public void Update(GameTime theGameTime)
{
KeyboardState aCurrentKeyboardState = Keyboard.GetState();
UpdateMovement(aCurrentKeyboardState);
UpdateJump(aCurrentKeyboardState);
UpdateDuck(aCurrentKeyboardState);
UpdateFireball(theGameTime, aCurrentKeyboardState);
mPreviousKeyboardState = aCurrentKeyboardState;
base.Update(theGameTime, mSpeed, mDirection);
}
private void UpdateMovement(KeyboardState aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
mSpeed = Vector2.Zero;
mDirection = Vector2.Zero;
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
mSpeed.X = PLAYER_SPEED;
mDirection.X = MOVE_LEFT;
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
mSpeed.X = PLAYER_SPEED;
mDirection.X = MOVE_RIGHT;
}
}
}
private void UpdateJump(KeyboardState aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
if (aCurrentKeyboardState.IsKeyDown(Keys.Space) == true && mPreviousKeyboardState.IsKeyDown(Keys.Space) == false)
{
Jump();
}
}
if (mCurrentState == State.Jumping)
{
if (mStartingPosition.Y - Position.Y > 85)
{
mDirection.Y = MOVE_DOWN;
}
if (Position.Y > mStartingPosition.Y)
{
Position.Y = mStartingPosition.Y;
mCurrentState = State.Walking;
mDirection = Vector2.Zero;
}
}
}
private void Jump()
{
if (mCurrentState != State.Jumping)
{
mCurrentState = State.Jumping;
mStartingPosition = Position;
mDirection.Y = MOVE_UP;
mSpeed = new Vector2(PLAYER_SPEED, PLAYER_SPEED);
}
}
private void UpdateDuck(KeyboardState aCurrentKeyboardState)
{
if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
{
Duck();
}
else
{
StopDucking();
}
}
private void Duck()
{
if (mCurrentState == State.Walking)
{
mSpeed = Vector2.Zero;
mDirection = Vector2.Zero;
Source = new Rectangle(219, 0, 28, 56); //We can use Source.Height instead "56" too...
mCurrentState = State.Ducking;
}
}
private void StopDucking()
{
if (mCurrentState == State.Ducking)
{
Source = new Rectangle(0, 0, 28, 56); //We can use Source.Height instead "56" too...
mCurrentState = State.Walking;
}
}
private void UpdateFireball(GameTime theGameTime, KeyboardState aCurrentKeyboardState)
{
foreach (Fireball aFireball in mFireballs)
{
aFireball.Update(theGameTime);
}
if (aCurrentKeyboardState.IsKeyDown(Keys.F) == true && mPreviousKeyboardState.IsKeyDown(Keys.F) == false)
{
ShootFireball();
}
}
private void ShootFireball()
{
if (mCurrentState == State.Walking)
{
bool aCreateNew = true;
foreach (Fireball aFireball in mFireballs)
{
if (aFireball.Visible == false)
{
aCreateNew = false;
aFireball.Fire(Position + new Vector2(Size.Width / 2, Size.Height / 2),
new Vector2(200, 0), new Vector2(1, 0));
break;
}
}
if (aCreateNew == true)
{
Fireball aFireball = new Fireball();
aFireball.LoadContent(mContentManager);
aFireball.Fire(Position + new Vector2(Size.Width / 2, Size.Height / 2),
new Vector2(200, 200), new Vector2(1, 0));
mFireballs.Add(aFireball);
}
}
}
public override void Draw(SpriteBatch theSpriteBatch)
{
foreach (Fireball aFireball in mFireballs)
{
aFireball.Draw(theSpriteBatch);
}
base.Draw(theSpriteBatch);
}
}
}
#endregion
#endregion
Any suggestions?
I LOVE VISUAL STUDIO ULTIMATE 2012!!! (Since I was coding with the express versions, now when I have the brand new one its great!
Continue reading...
My Sprite.cs code:
#region Sprite.cs Code
#region Using Statements
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.Audio;
#endregion
#region Sprite Logic
namespace My project
{
class Sprite
{
//The Rectangular area from the original image that defines the Sprite.
Rectangle mSource;
public Rectangle Source
{
get { return mSource; }
set
{
mSource = value;
Size = new Rectangle(0, 0, (int)(mSource.Width * Scale), (int)(mSource.Height * Scale));
}
}
//The current position of the Sprite
public Vector2 Position = new Vector2(0, 0);
//The texture object used when drawing the sprite
private Texture2D mSpriteTexture;
//The asset name for the Sprites Texture
public string AssetName;
//The Size of the Sprite (with scale applied)
public Rectangle Size;
//The amount to increase/decrease the size of the original sprite. When
//modified throught he property, the Size of the sprite is recalculated
//with the new scale applied.
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)(Source.Width * Scale), (int)(Source.Height * Scale));
}
}
//Load the texture for the sprite using the Content Pipeline
public void LoadContent(ContentManager theContentManager, string theAssetName)
{
mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
AssetName = theAssetName;
Source = new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height);
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
//Recalculate the Size of the Sprite with the new scale
Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
}
//Update the Sprite and change its position based on the passed in speed, direction and elapsed time.
public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection)
{
Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
}
//Draw the sprite to the screen
public virtual void Draw(SpriteBatch theSpriteBatch)
{
theSpriteBatch.Draw(mSpriteTexture, Position, Source,
Color.White, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
}
}
}
#endregion
#endregion
And this is my Player.cs code:
#region Player Code
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Microsoft.Xna.Framework.Audio;
#endregion
#region Player Logic
namespace My Project
{
class Player : Sprite
{
List<Fireball> mFireballs = new List<Fireball>();
ContentManager mContentManager;
Vector2 mStartingPosition = Vector2.Zero;
enum State
{
Walking,
Jumping,
Ducking
}
State mCurrentState = State.Walking;
Vector2 mDirection = Vector2.Zero;
Vector2 mSpeed = Vector2.Zero;
KeyboardState mPreviousKeyboardState;
const string PLAYER_ASSETNAME = "player-3";
const int START_POSITION_X = 10;
const int START_POSITION_Y = 350;
const int PLAYER_SPEED = 160;
const int MOVE_UP = -1;
const int MOVE_DOWN = 1;
const int MOVE_LEFT = -1;
const int MOVE_RIGHT = 1;
public void LoadContent(ContentManager theContentManager)
{
mContentManager = theContentManager;
foreach (Fireball aFireball in mFireballs)
{
aFireball.LoadContent(theContentManager);
}
Position = new Vector2(START_POSITION_X, START_POSITION_Y);
base.LoadContent(theContentManager, PLAYER_ASSETNAME);
Source = new Rectangle(0, 0, 28, 56); //We cant use Source.Height instead "56" here...
}
public void Update(GameTime theGameTime)
{
KeyboardState aCurrentKeyboardState = Keyboard.GetState();
UpdateMovement(aCurrentKeyboardState);
UpdateJump(aCurrentKeyboardState);
UpdateDuck(aCurrentKeyboardState);
UpdateFireball(theGameTime, aCurrentKeyboardState);
mPreviousKeyboardState = aCurrentKeyboardState;
base.Update(theGameTime, mSpeed, mDirection);
}
private void UpdateMovement(KeyboardState aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
mSpeed = Vector2.Zero;
mDirection = Vector2.Zero;
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
mSpeed.X = PLAYER_SPEED;
mDirection.X = MOVE_LEFT;
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
mSpeed.X = PLAYER_SPEED;
mDirection.X = MOVE_RIGHT;
}
}
}
private void UpdateJump(KeyboardState aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
if (aCurrentKeyboardState.IsKeyDown(Keys.Space) == true && mPreviousKeyboardState.IsKeyDown(Keys.Space) == false)
{
Jump();
}
}
if (mCurrentState == State.Jumping)
{
if (mStartingPosition.Y - Position.Y > 85)
{
mDirection.Y = MOVE_DOWN;
}
if (Position.Y > mStartingPosition.Y)
{
Position.Y = mStartingPosition.Y;
mCurrentState = State.Walking;
mDirection = Vector2.Zero;
}
}
}
private void Jump()
{
if (mCurrentState != State.Jumping)
{
mCurrentState = State.Jumping;
mStartingPosition = Position;
mDirection.Y = MOVE_UP;
mSpeed = new Vector2(PLAYER_SPEED, PLAYER_SPEED);
}
}
private void UpdateDuck(KeyboardState aCurrentKeyboardState)
{
if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
{
Duck();
}
else
{
StopDucking();
}
}
private void Duck()
{
if (mCurrentState == State.Walking)
{
mSpeed = Vector2.Zero;
mDirection = Vector2.Zero;
Source = new Rectangle(219, 0, 28, 56); //We can use Source.Height instead "56" too...
mCurrentState = State.Ducking;
}
}
private void StopDucking()
{
if (mCurrentState == State.Ducking)
{
Source = new Rectangle(0, 0, 28, 56); //We can use Source.Height instead "56" too...
mCurrentState = State.Walking;
}
}
private void UpdateFireball(GameTime theGameTime, KeyboardState aCurrentKeyboardState)
{
foreach (Fireball aFireball in mFireballs)
{
aFireball.Update(theGameTime);
}
if (aCurrentKeyboardState.IsKeyDown(Keys.F) == true && mPreviousKeyboardState.IsKeyDown(Keys.F) == false)
{
ShootFireball();
}
}
private void ShootFireball()
{
if (mCurrentState == State.Walking)
{
bool aCreateNew = true;
foreach (Fireball aFireball in mFireballs)
{
if (aFireball.Visible == false)
{
aCreateNew = false;
aFireball.Fire(Position + new Vector2(Size.Width / 2, Size.Height / 2),
new Vector2(200, 0), new Vector2(1, 0));
break;
}
}
if (aCreateNew == true)
{
Fireball aFireball = new Fireball();
aFireball.LoadContent(mContentManager);
aFireball.Fire(Position + new Vector2(Size.Width / 2, Size.Height / 2),
new Vector2(200, 200), new Vector2(1, 0));
mFireballs.Add(aFireball);
}
}
}
public override void Draw(SpriteBatch theSpriteBatch)
{
foreach (Fireball aFireball in mFireballs)
{
aFireball.Draw(theSpriteBatch);
}
base.Draw(theSpriteBatch);
}
}
}
#endregion
#endregion
Any suggestions?
I LOVE VISUAL STUDIO ULTIMATE 2012!!! (Since I was coding with the express versions, now when I have the brand new one its great!
Continue reading...