Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sprite rect for animations makes the character change position  (Read 867 times)

0 Members and 1 Guest are viewing this topic.

AzkaIsHere

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
sprite rect for animations makes the character change position
« on: February 02, 2018, 01:04:37 pm »
help guys I am trying to get an animated sprite for the character of my game that gets a rect of the sprite which changes if he goes right,left,up or down.
here is the code:

            if (e.key.code == sf::Keyboard::W) {
               character.setTextureRect(IntRect(-30,97,60,30));
               character.move(0, -5);

            }

the problem here is that when I press "w" the character changes position a bit to the right and doesn`t stay in the position where the previous frame of the animation was, the movement works correctly after it goes right. hulp plz.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sprite rect for animations makes the character change position
« Reply #1 on: February 03, 2018, 04:55:31 pm »
Guessing from your description, it sounds like your position of the sprite inside the rectangle may change. To keep it where you may expect it to be, you can either place you sprite within the rectangles to match or simply set the origin when you set the texture rectangle; the origin should be a point in each rectangle that matches the origin in the others.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*