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

Author Topic: This?  (Read 2130 times)

0 Members and 1 Guest are viewing this topic.

David

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
This?
« on: May 26, 2011, 03:34:30 am »
I'm really dumb when it comes to SFML :x

In a function, when you're trying to make a sprite change it's image, do you do "this.SetImage(Blah)"? It doesn't work, and I'm pretty sure there's something similar to that. :?

Also, how do you animate a sprite? I looked at the tutorial on the wiki but I wasn't able to understand it :evil:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
This?
« Reply #1 on: May 26, 2011, 08:03:10 am »
Quote
In a function, when you're trying to make a sprite change it's image, do you do "this.SetImage(Blah)"? It doesn't work, and I'm pretty sure there's something similar to that.

Code: [Select]
SetImage(...);
or
this->SetImage(...);


Quote
Also, how do you animate a sprite? I looked at the tutorial on the wiki but I wasn't able to understand it

You must have (at minimum) two parameters: the rectangles defining the animation frames in the source image (the spritesheet) and the time between them (ie. the animation speed).
With a sf::Clock, you measure the time elapsed. When time is greater than the frame duration, you switch to the next frame
Code: [Select]
void AnimatedSprite::Update()
{
    if (clock.GetElapsedTime() > frameDuration)
    {
        frame = (frame + 1) % nbFrames;
        sprite.SetSubRect(frames[frame]);
        clock.Reset();
    }
}

It's not complete and must be improved but I hope it helps you to understand.
Laurent Gomila - SFML developer

David

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
This?
« Reply #2 on: May 26, 2011, 01:31:30 pm »
Quote from: "Laurent"
Quote
In a function, when you're trying to make a sprite change it's image, do you do "this.SetImage(Blah)"? It doesn't work, and I'm pretty sure there's something similar to that.

Code: [Select]
SetImage(...);
or
this->SetImage(...);


Quote
Also, how do you animate a sprite? I looked at the tutorial on the wiki but I wasn't able to understand it

You must have (at minimum) two parameters: the rectangles defining the animation frames in the source image (the spritesheet) and the time between them (ie. the animation speed).
With a sf::Clock, you measure the time elapsed. When time is greater than the frame duration, you switch to the next frame
Code: [Select]
void AnimatedSprite::Update()
{
    if (clock.GetElapsedTime() > frameDuration)
    {
        frame = (frame + 1) % nbFrames;
        sprite.SetSubRect(frames[frame]);
        clock.Reset();
    }
}

It's not complete and must be improved but I hope it helps you to understand.


I understand, but, I'm not using a sprite sheet (it's individual images I made). But do you have to make it into a sprite sheet?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
This?
« Reply #3 on: May 26, 2011, 01:39:00 pm »
So instead of changing the subrect you change the image (SetImage).
Laurent Gomila - SFML developer

David

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
This?
« Reply #4 on: May 26, 2011, 02:38:17 pm »
Ah, now I get it.

I'm going to try this once I get home :D

 

anything