Nuffnang Ads

Thursday, 14 June 2012

XNA 2D game tutorial - controllable sprite

1. Initialize keyboard state (continue from XNA 2D game tutorial-animated sprite)

public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D character;
        Vector2 position = new Vector2(200, 200);
        Point frameSize = new Point(28, 41);
        Point currentFrame = new Point(1, 0);
        Point sheetSize = new Point(12, 8);
        int state = 2; // walk if keyboard pressed
        float speed = 5; 
        KeyboardState currentState;
        KeyboardState theKeyboardState;
        KeyboardState oldKeyboardState;
        TimeSpan nextFrameInterval = TimeSpan.FromSeconds((float)1 / 16);

------------------------------------------------------------------------------------------------------------------
protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            currentState = Keyboard.GetState();
            theKeyboardState = Keyboard.GetState();
            if (state == 1) // means walking state
            {
                currentFrame.Y = 2;
                currentFrame.X++;
                if (currentFrame.X >= 12)
                    currentFrame.X = 0;
            }
            oldKeyboardState = theKeyboardState;
            base.Update(gameTime);
        }

2. Move and animate
    if (state == 1) // means walking state
            {
                currentFrame.Y = 2;
                currentFrame.X++;
                if (currentFrame.X >= 12)
                    currentFrame.X = 0;
            }
else if (state == 2) // means wait for keyboard input
           {
                currentFrame.Y = 0;
                currentFrame.X ++;
                if (currentFrame.X >= 12)
                    currentFrame.X = 0;
                if (currentFrame.Y >= 1)
                {
                    currentFrame.Y = 1;
                    position.Y = 200;
                    frameSize = new Point(28, 41);
                }
                if (currentState.IsKeyDown(Keys.Right))
                {
                    frameSize = new Point(28, 41);
                    position.Y = 200;
                    currentFrame.Y = 2;
                    currentFrame.X++;
                    if (currentFrame.X >= 12)
                        currentFrame.X = 0;
                    position.X += speed;
                }
                else if (currentState.IsKeyDown(Keys.Left))
                {
                    frameSize = new Point(28, 41);
                    position.Y = 200;
                    currentFrame.Y = 1;
                    currentFrame.X++;
                    if (currentFrame.X >= 12)
                      currentFrame.X = 0;
                    position.X -= speed;
                }
            }
3. Run the code
4. If we press right,
5. If we press left,
6. Create .exe file
Build>> Publish <project name>.žChoose the location. Finish
7. VS 2010 will create your application’s installer
8. You can install your application in other computer ( just click the setup.exe file)
9. Click Start>> All programs. You will find your application has been installed in your computer.

10. You can refer the publication process from this link http://msdn.microsoft.com/en-us/library/bb464156.aspx









1 comment:

  1. Hello sir.. I tried your code on how to control the mario but the mario keep walking and input direction can not be detected.. can you suggest where should I look for the error.

    ReplyDelete