caragones Posted October 15, 2015 Share Posted October 15, 2015 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? Link to comment Share on other sites More sharing options...
jmp909 Posted October 15, 2015 Share Posted October 15, 2015 BitmapText uses getBitmapFont() https://github.com/photonstorm/phaser/blob/v2.4.3/src/gameobjects/BitmapText.js line 529 this._data = this.game.cache.getBitmapFont(this._font);what do you actually need to do? Link to comment Share on other sites More sharing options...
caragones Posted October 15, 2015 Author Share Posted October 15, 2015 I want to access the bitmapFont original size Link to comment Share on other sites More sharing options...
caragones Posted October 15, 2015 Author Share Posted October 15, 2015 TypeScript original definition of getBitmapFont:class Cache { ... getBitmapFont(key: string): Phaser.RetroFont; ....} Link to comment Share on other sites More sharing options...
jmp909 Posted October 15, 2015 Share Posted October 15, 2015 you can do this, I'm just trying to work out what object type will let you do that without using ['property'] notationvar b = this.game.cache.getItem('gem', Phaser.Cache.BITMAPFONT)console.log(b['base'].width); Link to comment Share on other sites More sharing options...
jmp909 Posted October 15, 2015 Share Posted October 15, 2015 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) it'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.tsclass 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 somethinghttps://github.com/photonstorm/phaser/blob/master/typescript/phaser.d.ts clark 1 Link to comment Share on other sites More sharing options...
clark Posted October 23, 2015 Share Posted October 23, 2015 Thanks for your help on this one guys, appreciated. I made a PR containing your findings. Link to comment Share on other sites More sharing options...
Tom Atom Posted October 23, 2015 Share Posted October 23, 2015 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; Link to comment Share on other sites More sharing options...
Recommended Posts