I made this following a tutorial and added some adjustments to it. You might have to change it around a little bit to load what you want etc etc.
PHP Code:
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;
public class Movement : Sprite
{
#region Declarings
const string MAIN_ASSETNAME = "Main_1";
const int START_POSITION_X = 125;
const int START_POSITION_Y = 429;
const int MOVE_SPEED = 160;
const int MOVE_UP = -1;
const int MOVE_DOWN = 1;
const int MOVE_LEFT = -1;
const int MOVE_RIGHT = 1;
int SCREEN_MAX_WIDTH = 758;
int SCREEN_MIN_WIDTH = 0;
int SCREEN_MAX_HEIGHT = 0;
int SCREEN_MIN_HEIGHT = 429;
public Boolean MOVE_DOWN_EN = false;
public Boolean MOVE_UP_EN = false;
SpriteFont font;
State mCurrentState = State.Walking;
Vector2 mDirection = Vector2.Zero;
Vector2 mSpeed = Vector2.Zero;
Vector2 mStartingPosition = Vector2.Zero;
KeyboardState mPreviousKeyboardState;
#endregion
enum State
{
Walking, Jumping
}
public void LoadContent(ContentManager theContentManager)
{
Position = new Vector2(START_POSITION_X, START_POSITION_Y);
base.LoadContent(theContentManager, MAIN_ASSETNAME);
}
public void Update(GameTime theGameTime)
{
KeyboardState aCurrentKeyboardState = Keyboard.GetState();
UpdateMovement(aCurrentKeyboardState);
UpdateJump(aCurrentKeyboardState);
mPreviousKeyboardState = aCurrentKeyboardState;
base.Update(theGameTime, mSpeed, mDirection);
}
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 > 50)
{
mDirection.Y = MOVE_DOWN;
}
if (Position.Y > mStartingPosition.Y)
{
Position.Y = mStartingPosition.Y;
mCurrentState = State.Walking;
mDirection = Vector2.Zero;
}
}
}
private void UpdateMovement(KeyboardState aCurrentKeyboardState)
{
if (mCurrentState == State.Walking)
{
mSpeed = Vector2.Zero;
mDirection = Vector2.Zero;
if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
{
mSpeed.X = MOVE_SPEED;
if (Position.X > SCREEN_MIN_WIDTH)
{
mDirection.X = MOVE_LEFT;
}
else
{
Position.X++;
}
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
{
mSpeed.X = MOVE_SPEED;
if (Position.X < SCREEN_MAX_WIDTH)
{
mDirection.X = MOVE_RIGHT;
}
else
{
Position.X--;
}
}
if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true && MOVE_UP_EN == true)
{
mSpeed.Y = MOVE_SPEED;
if (Position.Y > SCREEN_MAX_HEIGHT)
{
mDirection.Y = MOVE_UP;
}
else
{
Position.Y++;
}
}
else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true && MOVE_DOWN_EN == true)
{
mSpeed.Y = MOVE_SPEED;
if (Position.Y < SCREEN_MIN_HEIGHT)
{
mDirection.Y = MOVE_DOWN;
}
else
{
Position.Y--;
}
}
}
}
private void Jump()
{
if (mCurrentState != State.Jumping)
{
mCurrentState = State.Jumping;
mStartingPosition = Position;
mDirection.Y = MOVE_UP;
mSpeed = new Vector2(MOVE_SPEED, MOVE_SPEED);
}
}
}
Results 1 to 2 of 2
Thread: [C# XNA] Movement Class
- 25 Feb. 2011 10:14pm #1
[C# XNA] Movement Class
- 27 Feb. 2011 04:47pm #2
Cool. I did some stuff with XNA last year. You definitely want to have a movement class in which you can override trivial functions such as Jump() so you can re-use classes in different projects.