简体   繁体   中英

Basic animation doesn't fire

I'm trying to follow this example to create an animation:

http://seriss.com/people/erco/fltk/#Animate

Except for the fact that instead of changing the image I'm moving it. There's a car image that should move in a down-right direction every half second, for 10 times:

void func(void* data)
{
    static int counter=0;
    counter++;
    Fl_PNG_Image* image= static_cast<Fl_PNG_Image*>(data);
    Fl_Box* box= new Fl_Box(counter*10, counter*10,100,100);
    box->image(image);
    //delete box;
    window->redraw();
    if(counter==10)
    {
        Fl::remove_timeout(func,data);
    }
    else
    {
        Fl::repeat_timeout(.5,func,data);
    }
}

int main(int argc, char **argv)
{
    window = new Fl_Double_Window(width, height);

    Fl_PNG_Image* image= new Fl_PNG_Image("car-down.png");
    Fl_Box* box= new Fl_Box(0,0,100,100);
    box->image(image);
    Fl::add_timeout(.5, func, image);
    //delete box;

    window->end();
    window->show(argc, argv);
    return Fl::run();
}

I have two problems:

  1. I can't understand when I should delete the box. I'm using it to draw the image, and if I delete it immediately after having drawn the image, the image disappears.
  2. The image doesn't move, and it always stays on the initial position.

Instead of creating new boxes and images, move the box. The box will be deleted when the window is closed

void func(void* data)
{
    static int counter=0;
    counter++;
    //Fl_PNG_Image* image= static_cast<Fl_PNG_Image*>(data);
    Fl_Box* box= static_cast<Fl_Box*) data;
    box->position(counter*10, counter*10);
    window->redraw();
    ...
}

int main(int argc, char **argv)
{
    ...

    Fl::add_timeout(.5, func, box);

    window->end();
    window->show(argc, argv);
    return Fl::run();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM