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

Author Topic: How to do animation  (Read 9819 times)

0 Members and 1 Guest are viewing this topic.

Haikarainen

  • Guest
How to do animation
« Reply #15 on: August 26, 2011, 05:36:33 pm »
Dont expect anyone to just code for you. You're making this harder on yourself than it has to be. My advice; Create a resourcemanager, and then a class that handles animations.

asdatapel

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
How to do animation
« Reply #16 on: August 26, 2011, 10:10:19 pm »
animation. a proper example of animation. maybe of two things bieng animated at the same time?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
How to do animation
« Reply #17 on: August 26, 2011, 11:15:19 pm »
Have you looked at the wikis ? http://www.sfml-dev.org/wiki/en/sources/frame_anim_animated is only (my) an example.
SFML / OS X developer

asdatapel

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
back again
« Reply #18 on: September 01, 2011, 01:22:24 am »
soory guys for the break, but i was going back to basics with sfml. Anyways, I was doing clocks all over again and I did this, which was posted by one of the posters in this thread.
Code: [Select]
if (timer.GetElapsedTime() >= 1) {
            if (++whichOne > 2) {
                whichOne = 0;
                timer.Reset();
            }
        }

std::cout<<whichOne;



This is what it produces:





Its supposed to constantly say zero, one then two, for the same amount of time, but it does 0 for 1 second, then quickly 1 and 2. Help!

asdatapel

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
ugh
« Reply #19 on: September 01, 2011, 01:23:23 am »
also, why arnt the img tags working?

asdatapel

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
new problem
« Reply #20 on: September 01, 2011, 02:09:05 am »
lol never mind i got it. Aaaaaand theres a new problem. Every time I restart my computer or log off and back on, the loading images dont work. All that sows up is a white image. However, when i start a new project, the image loads properly, even if its the same exact code!!As soon as i log off, the project becomes corrupt, and i have to start a new one. PLZ HELP. im gonna cry...

asdatapel

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
figured it out
« Reply #21 on: September 01, 2011, 02:15:55 am »
So i found out why the image was white. When I run  the program from code::blocks, the image doesn't load. I tried it from the actual folder where the exe is and it works fine. I tried running code::blocks with as admin, but doesnt work. lol sorry for so many posts.

asdatapel

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
How to do animation
« Reply #22 on: September 01, 2011, 10:38:36 pm »
okay so i'm trying to make a animation by inheriting from sf::sprite.However, when I call App.Draw(Sprite), it shows up as a white box.

Oh yeah, this is not related to the problem above, which i found the soluton to.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
How to do animation
« Reply #23 on: September 02, 2011, 01:58:52 am »
Quote from: "asdatapel"
okay so i'm trying to make a animation by inheriting from sf::sprite.However, when I call App.Draw(Sprite), it shows up as a white box.

Oh yeah, this is not related to the problem above, which i found the soluton to.
The image used by the sprite is deleted before you draw it.
I use the latest build of SFML2

asdatapel

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
thanks....again
« Reply #24 on: September 02, 2011, 02:06:08 am »
wow sfml has a nice community.thanks for all the help. I finally got an animation class. I based it off the one above.
Code: [Select]

using namespace std;

class Animation : public sf::Sprite {

public:
    Animation(string location,int numFrames,float Delay);
    void setCurrentFrame(int frame);
    int getCurrentFrame();
    void setNumOfFrames(int frames);
    int getFrameWidth();
    void setDelay();
    int getDelay();
    void play();
    void update();
    void pause();
    void reset();

private:
    sf::Image image;
    sf::Clock timer;
    int currentFrame;
    int numOfFrames;
    int frameWidth;
    float frameTime;
};
Animation::Animation(string location,int numFrames,float Delay) {

    if (!image.LoadFromFile(location))
        std::cout<<"unable to load from "<<location;
    SetImage(image);

    currentFrame=1;
    numOfFrames=numFrames;
    frameWidth=GetSize().x/numOfFrames;
    frameTime=Delay;

    SetSubRect(sf::IntRect(0,0,frameWidth,GetSize().y));
}
void Animation:: setCurrentFrame(int frame) {
    currentFrame=frame;
}
int Animation::getCurrentFrame() {
    return currentFrame;
}
void Animation::setNumOfFrames(int frames) {
    numOfFrames=frames;
}
int Animation:: getFrameWidth() {
    return frameWidth;
}
void Animation::play() {
    SetSubRect(sf::IntRect((currentFrame-1)*frameWidth,0,currentFrame*frameWidth,GetSize().y));
}
void Animation::update() {
    if (timer.GetElapsedTime() >= frameTime) {
        if (++currentFrame > (numOfFrames))
            currentFrame = 1;
        timer.Reset();
    }
}

(ignore all the useless functions)