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

Author Topic: Animate sprite when moving it  (Read 2345 times)

0 Members and 1 Guest are viewing this topic.

GamingGod

  • Newbie
  • *
  • Posts: 6
    • View Profile
Animate sprite when moving it
« on: March 19, 2012, 06:19:07 pm »
Hi there.

I know it's kinda lame question, but I have to ask it. How I can animate sprite when I'm moving it? I'm using Thor for animation. I'm beginner and I can't figure it out. Now it works like that : animation starts playing, it stops, sprite is moving till I release key and then the last frame of animation is played. I want to make it even when key is hold sprite will move and animation will play. Here's my code (it's not the cleanest code ever, app made only for learning purposes) : http://goo.gl/ruhQ1
Hope anyone can help me. Cheers!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Animate sprite when moving it
« Reply #1 on: March 20, 2012, 12:38:14 pm »
If you specify nothing at thor::Action, the Realtime mode is specified, e.g. it is active as long as the key is held down. Therefore, PlayAnimation() starts the animation again and again, as long as you hold the right arrow key.

You should separate the actions to pressed key (plays the animation) and release key (stops the animation).
Code: [Select]
map[BeginRun] = Action(sf::Keyboard::Right, Action::PressOnce);
map[EndRun] = Action(sf::Keyboard::Right, Action::ReleaseOnce);

if (map.IsActive(BeginRun))
    animator.PlayAnimation(...);
else if (map.IsActive(EndRun))
    animator.StopAnimation(...);

By the way, one clock is enough. When you call GetElapsedTime(), you already have the difference since the last frame (because you restart the clock every frame).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

GamingGod

  • Newbie
  • *
  • Posts: 6
    • View Profile
Animate sprite when moving it
« Reply #2 on: March 20, 2012, 03:12:00 pm »
Thank you again, Nexus!

@Edit.
I did everything as you said (or maybe I'm retarded and I did not) but sprite is still moving while I'm holding the right arrow key, but animation is not playing. Here's my code : http://wklej.org/hash/0c59f97722d/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Animate sprite when moving it
« Reply #3 on: March 20, 2012, 06:14:29 pm »
Several points:
  • Call App.SetKeyRepeatEnabled(false);
  • If you want the animation to repeat, pass true to PlayAnimation()
  • Now the unit is only moved when the key is pressed (not held), i.e. you have to do something like
Code: [Select]
float dx = 0.f;
...
if (map.isActive(BeginRun))
{
dx = 3;
...
}
else if (map.isActive(EndRun))
{
dx = 0;
...
}

sprite.move(dx * 50.f * Time.asSeconds(),0.f);

  • sf::Event is not necessary ;)
By the way, there is still a possibility to use the approach you had before -- you can play the animation at startup and update the animator only while the key is held down.
Code: [Select]
animator.playAnimation("walk", true);
sf::Clock animClock, frameClock;

while(App.isOpen())
{
...

if (map.isActive(Run))
{
animator.update(animClock.restart());
animator.animate(sprite);
sprite.move(50.f * Time.asSeconds(),0.f);
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

GamingGod

  • Newbie
  • *
  • Posts: 6
    • View Profile
Animate sprite when moving it
« Reply #4 on: March 20, 2012, 08:27:19 pm »
Thank you so much, it's working right now.

 

anything