How do I select a sprite frame based on a range of values?

0 favourites
  • 5 posts
From the Asset Store
three fully animated characters ideal for your RPG side scroller projects
  • I have a sprite with 5 animation frames, each a different color;

    What i would like to do is select the a frame (from 0 to 4), based on a value from 0 to 100;

    so, if the value is 0-20, choose frame 0;

    if the value is 21-40, choose frame 1, and so on all the way up to 81-100;

    It feels that I should be using a statement with 'clamp' and modulus%20 to select the frame, but i can't figure out how;

    BTW, I know I can choose multiple if/else statements, but I think it can be done more efficiently;

    thanks;

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • modulus gives you the remainder after dividing. In this case you just want to divide.

    frame = clamp( floor( x / 20 ), 0, 4)

    (where x is 0 to 100, which is actually a range of 101. This breaks down to 0-19, 20-39, ... 80-100)

  • The range is 0 to 4, so you want: floor(min(x,99)/20)

  • the clamp was supposed to be 4, not 5, because 100 / 20 = 5, and you wanted to limit it to 4.

    frame = clamp( floor( x / 20 ), 0, 4)

    edit: yeah, using min works too :)

  • thanks, i was thinking about this and decided to go for a walk and came up with the following:

    frame = ceil(x/20)

    but your way works too!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)