collinhover / impactplusplus

Impact++ is a collection of additions to ImpactJS with full featured physics, dynamic lighting, UI, abilities, and more.
http://collinhover.github.com/impactplusplus
MIT License
276 stars 59 forks source link

UIMeter attempts to display nonexistent animation frame #58

Closed racingcow closed 11 years ago

racingcow commented 11 years ago

I was making a health meter today, and I think I may have stumbled onto a bug. It looks like UIMeter will try and show an animation frame that is outside the bounds of the animSettings array.

In my case, I have created a UIMeterHealth class that extends ig.UIMeter. My meter references a sprite sheet that contains 12 images, each of 32x32px (3 across and 4 down), and has animSettings like so...

animSettings: {
    main: {
        sequence: [0,1,2,3,4,5,6,7,8,9,10,11]
    }
}

On the UIMeter.draw method (line 374), it calculates the current frame based on the length of the animation sequence and this.value. The value can range from 0 to 1, inclusive. Whenever it is anything from 0 to 12 - 1 = 11, everything is fine. When it nears the max health of the player, however, this.value becomes 1, and frame gets set to 12, which is outside of the bounds of the array.

Adding this line seemed to fix it for me. Let me know if a PR would help and I can send one.

if (frame == this.currentAnim.sequence.length) frame--;
collinhover commented 11 years ago

Oops, that's a silly mistake. Thanks for catching! Line 374 should be:

var frame = Math.round( ( this.currentAnim.sequence.length - 1 ) * this.value);