quinton-ashley / p5play

JavaScript game engine that uses q5.js/p5.js for graphics and Box2D for physics.
https://p5play.org
GNU Affero General Public License v3.0
654 stars 183 forks source link

Issue with Tiles constructor #323

Closed azhen7 closed 4 months ago

azhen7 commented 4 months ago

After the most recent update (3.22.4) there is an issue with the Tiles constructor. If one of the Groups used in the Tiles constructor has a custom image used as the tile, the following error appears:

image

For example, the following code:

const g = new Group();
g.tile = "=";
g.img = loadImage("image.png"); //or whatever other valid image path

new Tiles(
  ["===="],
  0, 0,
  10, 10
);

will cause the aforementioned error.

I've traced the problem to line 641:

if (this._img || ani instanceof p5.Image) {
    this.image = new $.EmojiImage(ani, w);
    // ... rest of code

This code will be executed if ani is a p5.Image. However, the EmojiImage constructor treats the first argument (in this case, ani) as a String, and there is no case for if ani is a p5.Image. Thus, in _genTextImageKey, str (the first argument) should be a String but it is actually a p5.Image, and the error occurs.

I've found that if line 641 is changed to this.image = ani;, the issue is resolved.

EDIT: provided the Group has no animations, ani will always be an image by line 641. This is because of lines 613-627:

if (!group._isAllSpritesGroup) {
    if (!ani) {
        for (let _ani in group.animations) {
            ani = _ani;
            break;
        }
        if (!ani) {
            ani = group._img;
            if (typeof ani == 'function') {
                ani = ani(group.length);
            }
            if (ani) this._img = true;
        }
    }
}

By this point, ani will be undefined. After the for loop over group.animations, ani will still be undefined (since the Group has no animations). Because !ani will then evaluate true, ani will be assigned to group._img. Since the Group has a custom image, this means that ani will always be a p5.Image by line 641.

quinton-ashley commented 4 months ago

Thanks for reporting! Fixed in v3.22.5