phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
37.16k stars 7.1k forks source link

Tilesprite width / height not respected one dimension is zero #6857

Closed GaryStanton closed 4 months ago

GaryStanton commented 4 months ago

Version

Description

If you set a tilesprite with a zero width or height, and specify the other dimension - while it does take the dimensions from the texture, as it should, it doesn't tile the sprite to the other dimension.

So for example, if I have a texture that is 100x100px, I would expect the following code to result in a tile sprite that is 100px wide and 500px high, with my texture tiled five times vertically: this.add.tileSprite(0, 0, 0, 500, 'my100pxTexture')

However, it simply results in a tilesprite of 100x100px, matching the dimensions of the texture.

Example Test Code

https://phaser.io/sandbox/4nryhSBH

greptile-apps[bot] commented 4 months ago

For internal use only

To address the issue where a TileSprite does not tile correctly when one dimension is zero, modify the TileSprite class in TileSprite.js as follows:

  1. In the TileSprite constructor, update the width and height assignment to ensure tiling is applied correctly:
if (!width || !height) {
    width = width || displayFrame.width;
    height = height || displayFrame.height;
} else {
    width = Math.floor(width);
    height = Math.floor(height);
}
  1. In the setFrame method, ensure the canvas dimensions are updated correctly:
if (!newFrame.cutWidth || !newFrame.cutHeight) {
    this.renderFlags &= ~_FLAG;
} else {
    this.renderFlags |= _FLAG;
}

this.displayFrame = newFrame;

this.dirty = true;

this.updateTileTexture();

// Ensure the canvas dimensions are updated
this.canvas.width = this.width || newFrame.width;
this.canvas.height = this.height || newFrame.height;

return this;

These changes ensure that the TileSprite respects the specified dimension and tiles the texture correctly when one dimension is zero.

References

/src/gameobjects/tilesprite/TileSprite.js /src/gameobjects/tilesprite/TileSpriteFactory.js /src/gameobjects/tilesprite/TileSpriteCreator.js

#### About Greptile This response provides a starting point for your research, not a precise solution. Help us improve! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant. [Ask Greptile](https://app.greptile.com/chat/github/phaserjs/phaser/master) ยท [Edit Issue Bot Settings](https://app.greptile.com/apps/github)
zekeatchan commented 4 months ago

Hi @GaryStanton. Thanks for submitting this issue. We have fixed this and pushed it to the master branch. It will be part of the next release. Do test it out and let us know if you encounter any issues.

codingthat commented 2 days ago

@zekeatchan this appears to be a regression in 4.0.0-beta.1. You can verify this easily thanks to @GaryStanton 's sandbox MRE.