XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Silly question! but...

Last post 11/14/2009 3:39 PM by AnthonyMichael. 2 replies.
  • 11/14/2009 4:01 AM

    Silly question! but...

    So I have this function GetInput(KeyboardState prevKeyboardState)
    and it returns a Vector2 of a whichDirection variable.

    and I assign it to direction = GetInput(prevKeyboardState)

    but I also use this same function to check which direction my toon is facing when he's done walking ....
    I'll just show you the function.

            public Vector2 GetInput(KeyboardState prevKeyboardState)
            {

                currentKeyboardState = Keyboard.GetState();

                if (currentKeyboardState.IsKeyDown(Keys.Right))
                    whichDirection = new Vector2(MOVE_RIGHT, 0);
                if (currentKeyboardState.IsKeyDown(Keys.Left))
                    whichDirection = new Vector2(MOVE_LEFT, 0);
                if (!currentKeyboardState.IsKeyDown(Keys.Left) && !currentKeyboardState.IsKeyDown(Keys.Right))
                    whichDirection = Vector2.Zero;

                if (prevKeyboardState.IsKeyDown(Keys.Left))
                    currentState = State.FacingLeft;
                if (prevKeyboardState.IsKeyDown(Keys.Right))
                    currentState = State.FacingRight;
                
                return whichDirection;

            }

    So my issue is that to get prevKeyboardState, I have to declare it after I make a call to this function, but in my code I use this function again to discover which direction my toon is facing. But I call it like.

    GetInput(prevKeyboardState)

    So my question is, is that bad design? To not have the returned value "whichDirection" go anywhere? Fixing that would be easy... I know this, but I'm wondering if that is an improper way to use a return function.
  • 11/14/2009 5:47 AM In reply to

    Re: Silly question! but...

    Maybe it's better to split them up?

            public Vector2 GetDirectionFromInput(KeyboardState prevKeyboardState)
            {
                currentKeyboardState = Keyboard.GetState();

                if (currentKeyboardState.IsKeyDown(Keys.Right))
                    whichDirection = new Vector2(MOVE_RIGHT, 0);
                if (currentKeyboardState.IsKeyDown(Keys.Left))
                    whichDirection = new Vector2(MOVE_LEFT, 0);
                if (!currentKeyboardState.IsKeyDown(Keys.Left) && !currentKeyboardState.IsKeyDown(Keys.Right))
                    whichDirection = Vector2.Zero;
               
                return whichDirection;
            }

            public void SetToonStateFromInput(KeyboardState prevKeyboardState)
            {
                if (prevKeyboardState.IsKeyDown(Keys.Left))
                    currentState = State.FacingLeft;
                if (prevKeyboardState.IsKeyDown(Keys.Right))
                    currentState = State.FacingRight;
            }

    Game hobbyist hell-bent on coding a diabolical Matrix
  • 11/14/2009 3:39 PM In reply to

    Re: Silly question! but...

    I figured as much! Thanks.
Page 1 of 1 (3 items) Previous Next