Sign in to follow this  
caragones

TypeScript problem accessing BitmapFont Data

Recommended Posts

Hi!

 

I tryed to access BitmapFont data through 'game.cache.getBitmapFont()' but seems that typeScript file 'phaser.d.ts' does not define any class 'BitmapFont' and indicates that the function 'game.cache.getBitmapFont()' returns 'RetroFont' that is not correct because the function returns other thing.
 
I added this to 'phaser.d.ts':
 
class BMChar {    x: number;    y: number;    width: number;    height: number;    xOffset: number;    yOffset: number;    xAdvance: number;    kerning: any[];    texture: PIXI.Texture;}class BMFontData {    chars: any[];    font: string;    lineHeight: number;    size: number;}class BMFont {    base: PIXI.BaseTexture;    data: HTMLImageElement;    font: BMFontData;    url: string;}
and changed 'game.cache.getBitmapFont()' to return a BMFont.
 
Is this correct?
 
 

 

Share this post


Link to post
Share on other sites

i asked Rich in the Phaser Slack channel

 

(rich) fix the defs :wink:
easier than coding around it imho

(me).. cache.getBitmapFont returns eg 
Object {url: "assets/fonts/bitmapFonts/gem.png", data: img, font: Object, base: b.BaseTexture}

(rich) i
t's the actual font data, not a BitmapText object. 
data is an HTMLImage object. so a return type like this should work:
 
: { data: HTMLImageElement; font: any; url: string, base: PIXI.BaseTexture  };
 
then you could get it from either data.width or base.width

 

 

i think therefore you could add this to phaser.d.ts

class BitmapFont {        data: HTMLImageElement;     font: any;     url: string;    base: PIXI.BaseTexture;}class Cache {    ...    ...    getBitmapFont(key: string): Phaser.BitmapFont;    ...}

but actually maybe that should be something like BitmapFontData ?

 

 

also i've noticed 2 entries for cache add in the phaser.d.ts .. i'm assuming this isn't a mistake but just means you can construct it 2 different ways?

addBitmapFont(key: string, texture: Phaser.RetroFont): void;addBitmapFont(key: string, url: string, data: any, atlasData: any, atlasType: string, xSpacing?: number, ySpacing?: number): void;

we'd need to ensure not to mess up any internal workings!

 

this file is based on contributions though, so i guess you could propose something

https://github.com/photonstorm/phaser/blob/master/typescript/phaser.d.ts

 

 



 

Share this post


Link to post
Share on other sites

Hi, I needed the same. Here is how I did it:

            // font data            TextWrapper.fontData = aGame.cache.getBitmapFont(aFontName)["font"];            // if size not defined then take default size            if (aSize === undefined)                aSize = TextWrapper.fontData.size;

fontData is defined like:

static fontData: any;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.