Search code examples
c++allegroallegro5

Sprites; changing frames is not fluid in C++ Allegro 5


Watch the video: https://youtu.be/i2EXKY3EQPo The dragon movement is not fluid. It's like if all the frames were changing at the same time. What am I doing wrong?

shipImage = al_load_bitmap("dragon_stationary.png");
ship.maxFrame = 5;
ship.curFrame = 0;
ship.frameCount = 0;
ship.frameDelay = 50;
ship.frameWidth = 180;
ship.frameHeight = 126;
ship.animationColumns = 5;
ship.animationDirection = 1;
//this occurs every 1/60 of a second
void drawShip(SpaceShip &ship, ALLEGRO_BITMAP *flyingShip) {
    if (++ship.frameCount >= ship.frameDelay) {
        if (++ship.curFrame >= ship.maxFrame) {
            ship.curFrame = 0;
            ship.frameCount = 0;
        }
    }

    al_draw_bitmap_region(ship.image, ship.curFrame * ship.frameWidth, 0, ship.frameWidth, ship.frameHeight, ship.x, ship.y, 0);

This is the sprite:enter image description here


Solution

  • Try plotting out what happens for various values:

    | frameCount | curFrame |

    | ---------- | -------- |

    | 0 | 0 |

    | 1 | 0 |

    | 2 | 0 |

    | ... | ... |

    | 49 | 0 |

    | 50 | 1 |

    | 51 | 2 |

    | 52 | 3 |

    | 54 | 4 |

    | 55 | 5 |

    | 56 | 0 |

    | 0 | 0 |

    Note that when frameCount hits 50, it blasts through all your frames in sequence, then only resets once the animation is complete. You need to reset frameCount every timme it reaches frameDelay