Home XNA Tutorials XNA and the Chatpad
 
XNA and the Chatpad PDF Print E-mail
Written by Administrator   
Sunday, 27 September 2009 15:38

Update: I just copied this over from my old site, I will work on the formatting and make it easier to read.  PinoEire on the Creators website has also posted some great code on another way to solve this so I will ask if I can supply that code as well.

I have seen many games recently where the developers did not realize that the chatpad acts like a windows keyboard.  Then I went and did the same thing with one of my projects, there are several way to fix this. When you are developing your game, make sure you hide that code with something like #if !XBOX #else #endif.

Say for example you have cheat codes in place so you can warp between levels while you are designing your game, this code sure helps make things easier.  But then you release your game to peer review, it passes and it goes up for sale with all the "cheat codes" now in place.  Sure they were just harmless while you were testing, but now they are easter eggs and cheat codes. Cheat codes can be really unfair if your game has a leader-board.

At the same time, you can hide your developer only button presses, but expose the rest to the 360/Chatpad.

I am still new with Xna and programming in general, but I hide the keyboard commands from the Xbox with:

//If we are not playing on the 360 or Zune, ignore the following lines
#if !XBOX
// Windows movement commands
if (keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A))
{
level.Player.movement.X--;
}
else if (keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D))
{ 
level.Player.movement.X++;
}
if (keyboardState.IsKeyDown(Keys.Up) || keyboardState.IsKeyDown(Keys.W))
{
level.Player.movement.Y--;
}
else if (keyboardState.IsKeyDown(Keys.Down) || keyboardState.IsKeyDown(Keys.S))
{
level.Player.movement.Y++;
}
//Otherwise we must be playing on a 360 or a Zune
#else
//If you want to use a chatpad/keyboard command that will be seen on the 360, make sure it is inside here
//Xbox movement commands
if (gamePadState.IsButtonDown(Buttons.DPadLeft))
{
level.Player.movement.X--;
}
else if (gamePadState.IsButtonDown(Buttons.DPadRight))
{
level.Player.movement.X++;
}
if (gamePadState.IsButtonDown(Buttons.DPadUp))
{
level.Player.movement.Y--;
}
else if (gamePadState.IsButtonDown(Buttons.DPadDown))
{
level.Player.movement.Y++;
}
#endif

This example was taken from a platformer that I am working on, so I wanted to separate the "WASD" and arrow key presses from not actually making it to the Xbox.  Any code that you have that might be for skipping levels, adding lives, or whatever can be separated out in this way. If you plan on using the chatpad in your game, make sure you hide the "god mode" code at least.

There might be an easier way, if so let me know I would love to know as well.  You can also use #if WINDOWS (if we are playing on a windows machine) or #if !WINDOWS (which means if not windows, think of the ! as not or false) as well.  I will do another tutorial on ! in the near future.

For those new to XNA what we are doing is asking a question.  If we are playing the game on Windows, then we want to do everything between the # signs.   We start it by saying #if and end it simply with #endif - we can also use #else in the middle.   Very much like a traditional if then statement, but this code looks at what console it is being run on.  If you have done any of the starter kits you may have seen #if ZUNE as well.   The XNA team has made it really easy to do this, and you can use this to load different art/sound assets for the Zune over Windows/Xbox.

Last Updated on Sunday, 25 October 2009 04:50
 

Contact Us

A car usually has how many tires?
Email:
Subject:
Message:

XNA Books

Joomla Templates by Joomlashack