melonjs / melonJS

a fresh, modern & lightweight HTML5 game engine
https://melonjs.org
MIT License
5.92k stars 643 forks source link

Sprite framewidth/frameheight ignored after first use of image #1087

Closed dynamo-foundation closed 2 years ago

dynamo-foundation commented 2 years ago

If you are submitting a bug report, please respect the below template :

OS platform / Browser

Any

melonJS version

10

Bug description

See attached screencaps. If a sprite is created using an image and then a second sprite is created using the same image, the framewidth and frameheight settings are ignored on the second sprite. In the attached example, if I use "ui64" as the image source for the second sprite, the framewidth and frameheight are forced to 64 (the settings from the first sprite). This forces me to use a differently named copy of the sprite in order to get different framewidths/frameheights.

Steps to reproduce the bug

create a sprite with an image and specify a framewidth and frameheight create a second sprite with the same image and specify a different framewidth and frameheight dump the points array to the console for the second sprite - you will see the dimensions of the first sprite

Console log Extract

to show possible exception/errors raised by the engine or the browser

How to enable/use the console log
Capture1 Capture2
obiot commented 2 years ago

I'm not getting what you do exactly in the code above, but I create to me.Sprite object, using the same image, but specifying different frame width and frame height, I don;'t have any issue on my side...

obiot commented 2 years ago

oh wait, fuck yes, you are right ! holy cow this is weird 🤦 🥲

obiot commented 2 years ago

found the issue !

the problem is here: https://github.com/melonjs/melonJS/blob/master/src/video/texture_cache.js#L51

with the cached texture being retreived only based on the image itself (I mean HTMLImageElement), searching for the "right" texture cached with also width and height should fix it

obiot commented 2 years ago

and here is a fix for this one (see last commit), not super happy about it though, looks overcomplex (so I'll keep the ticket open for a little bit more), but working for now I guess

obiot commented 2 years ago

this one done, closing this ticket