samedi 28 janvier 2017

Pretty way to code a bunch of similar if statements in c++?

i want to animate a sprite. To do that i have a ticker that gives me a time in milliseconds. Depending on the time i want the sprite to show a different image. Unfortunately i can't think of a simple and pretty way to do this except for a bunch of if statements.

if (ticker_.getElapsedTime().asMilliseconds() >= ANIMATION_STEP * 0 && ticker_.getElapsedTime().asMilliseconds() < ANIMATION_STEP * 1)
{
    sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_00.png"));
}

if (ticker_.getElapsedTime().asMilliseconds() >= ANIMATION_STEP * 1 && ticker_.getElapsedTime().asMilliseconds() < ANIMATION_STEP * 2)
{
    sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_01.png"));
}

// 10 more of these ...

if (ticker_.getElapsedTime().asMilliseconds() >= ANIMATION_STEP * 13 && ticker_.getElapsedTime().asMilliseconds() < ANIMATION_STEP * 14)
{
    sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_13.png"));
}

Aucun commentaire:

Enregistrer un commentaire