Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GameObjects.BitmapText.setFont -> setFont function ignores arguments when the texture doesnt changes #6740

Closed
AlvaroNeuronup opened this issue Feb 15, 2024 · 2 comments

Comments

@AlvaroNeuronup
Copy link

Version

  • Phaser Version: 3.70.0

Description

Setting font on a BitmapText without changing the texture doesnt check the new font-size and ignores it.
Also ignores the align argument.

Expected behaviour

setFont('same-texture', 48);

should work the same as:

setFont('same-texture').setFontSize(48); // This works fine

Example Test Code

class Example extends Phaser.Scene {

    preload() {
        this.load.bitmapFont('desyrel', 'assets/fonts/bitmap/desyrel.png', 'assets/fonts/bitmap/desyrel.xml');
        this.load.bitmapFont('desyrel-pink', 'assets/fonts/bitmap/desyrel-pink.png', 'assets/fonts/bitmap/desyrel-pink.xml');
        this.load.bitmapFont('shortStack', 'assets/fonts/bitmap/shortStack.png', 'assets/fonts/bitmap/shortStack.xml');
    }

    create() {
        const b2 = this.add.bitmapText(400, 400, 'desyrel', 'Phaser test', 40);

        b2.setFont('desyrel', 12);
        console.warn(b2.fontSize); // It should be 12 but it is 40 

        // If the font changes it works fine
        // b2.setFont('shortStack', 12);
        // console.warn(b2.fontSize);
    }
}

const config = {
    type: Phaser.WEBGL,
    parent: 'phaser-example',
    scene: Example
};

const game = new Phaser.Game(config);
@UnaiNeuronUp
Copy link

It seems to work with this code (i don't know if it is the best solution):

setFont: function (key, size, align)
    {
      if (size === undefined) { size = this._fontSize; }
      if (align === undefined) { align = this._align; }

      if (key !== this.font)
      {
          var entry = this.scene.sys.cache.bitmapFont.get(key);

          if (entry)
          {
              this.font = key;
              this.fontData = entry.data;
              this.fromAtlas = entry.fromAtlas === true;

              this.setTexture(entry.texture, entry.frame);
          }
      }
    
      this._fontSize = size;
      this._align = align;
      GetBitmapTextSize(this, false, true, this._bounds);

      return this;
    },

@photonstorm
Copy link
Collaborator

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants