8 Replies - 19901 Views - Last Post: 29 November 2009 - 01:42 PM

#1 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 190
  • Joined: 15-August 09

XNA 2D SPRITE JUMP

Posted 25 November 2009 - 11:32 PM

Hi i am trying to figure out a way to make my 2D sprite jump but i coulndn't find any good tutorial to do it.

:/





 
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace sada
{
	/// <summary>
	/// This is the main type for your game
	/// </summary>
	public class Game1 : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		SpriteBatch spriteBatch;

		Texture2D character;
		Point frameSize = new Point(48, 48);
		Point currentFrame = new Point(0, 0);
		Point sheetSize = new Point(3, 11);
		
		float speed = 4;
			  
		Vector2 Position = new Vector2(300, 300);
	   

		public Game1()
		{
			graphics = new GraphicsDeviceManager(this);
			Content.RootDirectory = "Content";
			TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 100);
		}

		/// <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);
			character = Content.Load<Texture2D>("c");
			// 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 aCurrentKeyboardState = Keyboard.GetState();



			
		   



		   
			

			if (aCurrentKeyboardState.IsKeyDown(Keys.Right))
			{
				currentFrame.Y = 7;
				Position.X +=speed;
				++currentFrame.X;
				if (currentFrame.X >= sheetSize.X)
				{
					currentFrame.X = 0;
				}
			}
			if (aCurrentKeyboardState.IsKeyDown(Keys.Left))
			{
				Position.X -= speed;
				currentFrame.Y = 6;
				++currentFrame.X;
				if (currentFrame.X >= sheetSize.X)
				{
					currentFrame.X = 0;
				}
			}
			

			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
			spriteBatch.Begin();
			spriteBatch.Draw(character, Position, new Rectangle(
				frameSize.X * currentFrame.X,
				frameSize.Y * currentFrame.Y,
				frameSize.X,
				frameSize.Y),
				Color.White
					, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
			spriteBatch.End();
			base.Draw(gameTime);
		}
	}
}




It is supposed when you press a key the sprite position on Y is updating and when it reaches the appropriate height
you have put minus the position at Y.


if (aCurrentKeyboardState.IsKeyDown(Keys.Up))
{
	 Position.Y -= speed;
		 if(PositionY >= 150)
		  {
			 Position.Y += speed;
		  }

}



but it doesnt work :/ any help?

This post has been edited by Hellbroth: 26 November 2009 - 01:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: XNA 2D SPRITE JUMP

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1000
  • View blog
  • Posts: 4,181
  • Joined: 14-February 08

Re: XNA 2D SPRITE JUMP

Posted 26 November 2009 - 03:43 AM

Out of curiosity, what does the sprite do when you use this? does it jump and never stop? or does it not jump at all?
Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: XNA 2D SPRITE JUMP

Posted 26 November 2009 - 06:01 AM

You can check out this tutorial. It goes over the basics of making a sprite jump.
http://www.xnadevelo...rdjumping.shtml
Was This Post Helpful? 0
  • +
  • -

#4 Theaegd  Icon User is offline

  • Hater & Lover

Reputation: -125
  • View blog
  • Posts: 944
  • Joined: 15-August 09

Re: XNA 2D SPRITE JUMP

Posted 26 November 2009 - 10:11 AM

Well, make variable to store the state of the player. If the player state is jumping then some collisons are cancelled out until the state is normal again. This would be if the player was jumping over an object that you normally cant pass through.
Was This Post Helpful? 0
  • +
  • -

#5 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 190
  • Joined: 15-August 09

Re: XNA 2D SPRITE JUMP

Posted 27 November 2009 - 03:32 PM

Thanx guys problem solved and this time everything is more Object oriented :) time by time i am getting more into
OOP and this is realy good Classes are way to powerfull and i should learn more about them :).
Was This Post Helpful? 0
  • +
  • -

#6 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: XNA 2D SPRITE JUMP

Posted 27 November 2009 - 03:35 PM

View PostHellbroth, on 27 Nov, 2009 - 04:32 PM, said:

Thanx guys problem solved and this time everything is more Object oriented :) time by time i am getting more into
OOP and this is realy good Classes are way to powerfull and i should learn more about them :).


The larger and more complex your games get the more and more you should use classes. Especially learn and understand how inheritance and polymorphism work.
Was This Post Helpful? 0
  • +
  • -

#7 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 190
  • Joined: 15-August 09

Re: XNA 2D SPRITE JUMP

Posted 29 November 2009 - 02:09 AM

Problem Solved.
Thanx to http://www.xnadevelo...tutorials.shtml .
This is the character i used i hope it helps other too.

Posted Image

 
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame17
{
	/// <summary>
	/// This is the main type for your game
	/// </summary>
	public class Game1 : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		SpriteBatch spriteBatch;
		Texture2D character;
		Point currentFrame = new Point(0, 5);
		Point frameSize = new Point(48, 48);
		Point sheetSize = new Point(3, 21);
		Vector2 Position = new Vector2(100, 100);
		int START_POSITION_X = 125;
		int START_POSITION_Y = 245;
		int CHARACTER_SPEED = 160;
		int MOVE_UP = -1;
		int MOVE_DOWN = 1;
		int MOVE_LEFT = -1;
		int MOVE_RIGHT = 1;
		enum State
		{

			Walking,
			Jumping
		}

		State mCurrentState = State.Walking;

		Vector2 mDirection = Vector2.Zero;

		Vector2 mSpeed = Vector2.Zero;
		KeyboardState mPreviousKeyboardState;
		Vector2 mStartingPosition = Vector2.Zero;

		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();
		}

		/// <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);
			character = Content.Load<Texture2D>("character");
			Position = new Vector2(START_POSITION_X, START_POSITION_Y);

			// 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 aCurrentKeyboardState = Keyboard.GetState();

			Position += mDirection * mSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

			UpdateMovement(aCurrentKeyboardState);
			UpdateJump(aCurrentKeyboardState);


			mPreviousKeyboardState = aCurrentKeyboardState;

			base.Update(gameTime);
		}
		private void UpdateMovement(KeyboardState aCurrentKeyboardState)
		{


			if (mCurrentState == State.Walking)
			{

				mSpeed = Vector2.Zero;

				mDirection = Vector2.Zero;



				if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
				{

					mSpeed.X = CHARACTER_SPEED;

					mDirection.X = MOVE_LEFT;

				}

				else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
				{

					mSpeed.X = CHARACTER_SPEED;

					mDirection.X = MOVE_RIGHT;

				}



				if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
				{

					mSpeed.Y = CHARACTER_SPEED;

					mDirection.Y = MOVE_UP;

				}

				else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
				{

					mSpeed.Y = CHARACTER_SPEED;

					mDirection.Y = MOVE_DOWN;

				}

			}

		}
		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 > 150)
				{

					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(CHARACTER_SPEED, CHARACTER_SPEED);

			}

		}






		/// <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);

			spriteBatch.Begin();
			spriteBatch.Draw(character, Position, new Rectangle(
				frameSize.X * currentFrame.X,
				frameSize.Y * currentFrame.Y,
				frameSize.X,
				frameSize.Y),
				Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
			spriteBatch.End();

			base.Draw(gameTime);
		}
	}
}



This post has been edited by Hellbroth: 29 November 2009 - 02:23 AM

Was This Post Helpful? 2
  • +
  • -

#8 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Re: XNA 2D SPRITE JUMP

Posted 29 November 2009 - 01:21 PM

You know, I didn't have to bother with OOP-enforced languages the first couple of years I was writing video games. It just seems too needlessly complicated for a beginner to learn.
Was This Post Helpful? 0
  • +
  • -

#9 Theaegd  Icon User is offline

  • Hater & Lover

Reputation: -125
  • View blog
  • Posts: 944
  • Joined: 15-August 09

Re: XNA 2D SPRITE JUMP

Posted 29 November 2009 - 01:42 PM

i like your sig pic sixofeleven
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1