0
votes

i just got into game development and XNA and was following a tutorial and decided to try and add in a bound area for a floor. In the tutorial the sprite could move freely and i wanted to have a stopping point for it, so i added a statement to part of the input method

if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
            {
                if (this.Position.Y == 420)
                {
                    MOVE_DOWN = 0;
                    mDirection.Y = MOVE_DOWN;
                }

                else
                {
                    mSpeed.Y = PLAYER_SPEED;
                    MOVE_DOWN = 1;
                    mDirection.Y = MOVE_DOWN;
                }


            }

MOVE_DOWN is my variable for the y change, if it = 0, there is no movement, 1 it moves down, -1 it moves up. this worked only if the position of the bounds(420) was equal to the position that my sprite started out at, other than that it doesnt work.

i think its because the position isnt updating correctly. i dont know ive tried a lot of things and am pretty new with XNA and game development. Any help would be greatly appreciated.

Here is the update method for my player sprite

 public void Update(GameTime theGameTime)

    {

        KeyboardState aCurrentKeyboardState = Keyboard.GetState();



        UpdateMovement(aCurrentKeyboardState);




        mPreviousKeyboardState = aCurrentKeyboardState;



        base.Update(theGameTime, mSpeed, mDirection);

    }

and here is the update for the base class

public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection)
    {
        Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
    }
1
I don't see anything at all indicating that you are using a bounding box (or circle). If you could post the code for the bounding box movement, that would help a lot more.Darkhydro
i dont know if i used the correct terminology, but i just wanted a line that the sprite couldnt cross. a boundary at y = 420.Stone

1 Answers

0
votes

If 'Position' is a Vector2, then it is using floats of the X & Y components.

For practical purposes, a float will rarely equal a whole number due to floating point rounding errors.

if (this.Position.Y == 420)//will never return true

should be changed to:

if (this.Position.Y < 420)
{
   this.Position = 420;
   //other stuff you have
}