0

I've been working on a really simple game as a base for a project in my CS course, but while mapping the movement commands, I keep having trouble making the character sprite move upwards as well as swap textures. Whenever I try moving upwards, the character moves up one space and swaps textures, then resets to the previous position when I release the button. I've tried a few different methods and I end up with pretty much the same result. Any suggestions would be greatly appreciated.

public static void Update ()
    {
        // Query gamepad for current state
        var gamePadData = GamePad.GetData (0);
        pnut = pnutDefault;
        pnutDown.Position = pnut.Position;

        if ((gamePadData.Buttons & GamePadButtons.Left) != 0)
        {
            pnut.Position.X--;
        }

        if ((gamePadData.Buttons & GamePadButtons.Right) != 0)
        {
            pnut.Position.X++;
        }

        if ((gamePadData.Buttons & GamePadButtons.Down) != 0)
        {
            pnut.Position.Y++;
        }

            if ((gamePadData.Buttons & GamePadButtons.Up) != 0)
        {
            //pnutDown.Position = pnut.Position;

            pnutDown.Position.Y--;
            pnut.Position.Y--;

        }
    }
  • Where do you define pnutDefault? – Adam Zuckerman Mar 05 '14 at 01:09
  • The Initialize method `pnut = new Sprite(graphics, t); pnutDefault = pnut;` – FinalFormLuigi Mar 05 '14 at 01:28
  • Those might help to debug the issue you are having. Please edit your question and add the appropriate code. – Adam Zuckerman Mar 05 '14 at 01:30
  • @AdamZuckerman Your comment reminded me that the pnutDefault position was not being updated with the pnutDown sprite causing it to revert to the previous position each update. The new code is as follows: – FinalFormLuigi Mar 05 '14 at 01:33
  • `public static void Update () { var gamePadData = GamePad.GetData (0); pnut = pnutDefault; pnutDown.Position = pnut.Position; if ((gamePadData.Buttons & GamePadButtons.Left) != 0){ pnut.Position.X--; } if ((gamePadData.Buttons & GamePadButtons.Right) != 0){ pnut.Position.X++; } if ((gamePadData.Buttons & GamePadButtons.Down) != 0){ pnut.Position.Y++; }if ((gamePadData.Buttons & GamePadButtons.Up) != 0){ //pnutDown.Position = pnut.Position; pnut = pnutDown; pnutDown.Position.Y--; pnutDefault.Position = pnutDown.Position; }` – FinalFormLuigi Mar 05 '14 at 01:35
  • ok im not sure how to format that...sorry – FinalFormLuigi Mar 05 '14 at 01:36
  • There is an edit button just below your original question (share | edit | flag). Click there to edit your question. – Adam Zuckerman Mar 05 '14 at 01:36

0 Answers0