3 Replies - 3331 Views - Last Post: 11 January 2010 - 12:12 PM

#1 Kratos61  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 31-October 09

Controls in C# XNA game

Post icon  Posted 11 January 2010 - 09:07 AM

I'm making a game in XNA using c# and I'm wondering if its possible to make it so that I can control the 3d model in the game with a keyboard and an Xbox 360 controller my code for the XBox controller that i'm using is:
   private void ControllerManager(GamePadState aControllerState, ref PlayerShip aShip, GameTime gameTime)
		{
			// check for input from a player

			// left and right
			if (aControllerState.ThumbSticks.Left.X > 0 || aControllerState.DPad.Right > 0)
			{
				aSlime.IncrimentalMove(new Vector3(GameParameters.movementSpeed, 0.0f, 0.0f));
			}
			else if (aControllerState.ThumbSticks.Left.X < 0 || aControllerState.DPad.Left > 0)
			{
				aSlime.IncrimentalMove(new Vector3(-GameParameters.movementSpeed, 0.0f, 0.0f));
			}

			if (aControllerState.Buttons.A == ButtonState.Pressed)
			{
				if (aSlime.Position.Y < (GameParameters.bottomBound + 1.0f))
				{
					aSlime.AddVelocity(GameParameters.slimesJumpingVelocity); 
				}
			   
			}

			if (aControllerState.Buttons.B == ButtonState.Pressed && 
				aSlime.Position.Y > (GameParameters.bottomBound + 1.0f))
			{
				float currentGameTime = (float)gameTime.TotalGameTime.TotalMilliseconds;

				if (aSlime.LastTimeIShot + 500.0f < currentGameTime)
				{
					// create a simgle slime bullet
					Laser aSingleBullet = new Laser();
					aSingleBullet.ThreeDModelFile = Content.Load<Model>("Models/SlimeManNum1");
					aSingleBullet.ScaleValue = 0.4f;
					aSingleBullet.Velocity = new Vector3(20.0f, 0.0f, 0.0f);
					aSingleBullet.Position = greenSlime.Position;

					// add the 1 bullet to the list
					greenSlimeBullet.Add(aSingleBullet);
					aSlime.LastTimeIShot = (float)gameTime.TotalGameTime.TotalMilliseconds;
				}
			}
		}


Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Controls in C# XNA game

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 929
  • View blog
  • Posts: 6,316
  • Joined: 18-October 08

Re: Controls in C# XNA game

Posted 11 January 2010 - 09:36 AM

You are in the wrong forum for this. Moving this to the XNA forum.
Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 929
  • View blog
  • Posts: 6,316
  • Joined: 18-October 08

Re: Controls in C# XNA game

Posted 11 January 2010 - 09:41 AM

Yes, you should be able to move the model using the keyboard as well as the controller. If you have a KeyboadState field called keyboardState you should be able to do something like the following.

			if (keyboarState.IsKeyDown(Keys.Right) || aControllerState.ThumbSticks.Left.X > 0 || aControllerState.DPad.Right > 0)
			{
				aSlime.IncrimentalMove(new Vector3(GameParameters.movementSpeed, 0.0f, 0.0f));
			}
			else if keyboarState.IsKeyDown(Keys.Left) || (aControllerState.ThumbSticks.Left.X < 0 || aControllerState.DPad.Left > 0)
			{
				aSlime.IncrimentalMove(new Vector3(-GameParameters.movementSpeed, 0.0f, 0.0f));
			}

			if (keyboarState.IsKeyDown(Keys.Space) || aControllerState.Buttons.A == ButtonState.Pressed)
			{
				if (aSlime.Position.Y < (GameParameters.bottomBound + 1.0f))
				{
					aSlime.AddVelocity(GameParameters.slimesJumpingVelocity);
				}
			   
			}

			if (aControllerState.Buttons.B == ButtonState.Pressed &&
				aSlime.Position.Y > (GameParameters.bottomBound + 1.0f))
			{
				float currentGameTime = (float)gameTime.TotalGameTime.TotalMilliseconds;

				if (aSlime.LastTimeIShot + 500.0f < currentGameTime)
				{
					// create a simgle slime bullet
					Laser aSingleBullet = new Laser();
					aSingleBullet.ThreeDModelFile = Content.Load<Model>("Models/SlimeManNum1");
					aSingleBullet.ScaleValue = 0.4f;
					aSingleBullet.Velocity = new Vector3(20.0f, 0.0f, 0.0f);
					aSingleBullet.Position = greenSlime.Position;

					// add the 1 bullet to the list
					greenSlimeBullet.Add(aSingleBullet);
					aSlime.LastTimeIShot = (float)gameTime.TotalGameTime.TotalMilliseconds;
				}
			}



The Right key should be able to move the model right, the Left key should be able to move the model left and the space bar would work like the A button.
Was This Post Helpful? 0
  • +
  • -

#4 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: Controls in C# XNA game

Posted 11 January 2010 - 12:12 PM

Don't forget to put this on the top.Otherwise you will press the buttons but they won't do anything :)


keyboardState = Keyboard.GetState();



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1