Purpose of direction_key.x = abs(direction_key.x)

Juani (jiml)

I don't quite understand why we are making the value of x always positive. I would think it is to be used in the IF Statement that comes after. But in that IF, are we looking for a Vector2 that is equal to direction_key, or are we validating that number exists in the dictionary?

1 love
  • Nathan Lovato replied

    It is because of the keys in our dictionary. For example, Vector2.DOWN is equal to Vector2(0, 1). Our dictionary keys only have positive values.

    The reason is that we use our dictionary to pick textures. When we want the character to look to the left, we pick the texture of the character looking to the right and flip the sprite horizontally. We don't need a sprite of the character looking to the left, thus in our DIRECTION_TO_FRAME dictionary, we don't have a key with the direction vector pointing to the left (which would have an X value of -1).

    Because all our dictionary keys have only positive values, we need to convert our player's input direction into a vector with only positive values.

    Let me know if you have more questions because I understand this can be confusing.

    4 loves
  • j
    Juani (jiml) replied

    Thanks for the answer Nathan! With that information and the update on this lesson, now it's a lot more clear.

    1 love
  • Nathan Lovato replied

    Glad to hear this helped!