Phaser: Graphics improperly extends DisplayObject

Created on 22 May 2016  ·  15Comments  ·  Source: photonstorm/phaser

Hi there,

Looks like the signature for Graphics.generateTexture doesn't match that of DisplayObject.generateTexture. This is an issue for typescript compilation (see pixi.d.ts).

Thanks for the awesomeness y'all.

🤷‍♂️ More info needed

Most helpful comment

@JimmyBoh - I modified ../phaser/typescript/pixi.d.ts locally and changed the signature of Graphics.generateTexture to match that of DisplayObject.generateTexture:

generateTexture(resolution?: number, scaleMode?: number, renderer?: PixiRenderer): RenderTexture;

All 15 comments

Experienced the same problem when I tried to update to the latest version of Phaser.

Suggested fix? Because PIXI.DisplayObject.generateTexture returns a RenderTexture, where-as PIXI.Graphics.generateTexture _does_ return Texture. So they are actually different.

Then is it possible to rename Graphics.generateTexture? The current implementation prevents typescript compilation from succeeding. Another possible solution is to have them both return some common interface type.

I wish I was back on game work :(

Reminds me of my old stack question. http://stackoverflow.com/questions/29593905/typescript-declaration-extending-class-with-static-method/29595798#29595798

declare class DisplayObject {
    static generateTexture(): RenderTexture;
}

Include the same signature from the primary above to the other class and overload it.

declare class Graphics extends DisplayObject {
    static generateTexture(): RenderTexture;
    static generateTexture(): Texture;
}

Does it work?

Overloading the function sounds like a reasonable solution to me.

"Include the same signature from the primary above to the other class and overload it."

Except that both signatures would be visible and the 1st signature (from base class) wouldn't ever actually work as described. Renaming the PIXI.Graphics.generateTexture() might be best.

Yeah totally but that is a different issue. What you see today in JS is valid and correct JS. If TypeScript cannot define it, then you must work around it.

If you want to change the implementation, you have the much bigger job of making a PR which is going to be bug free, and not break 1000 peoples existing games for the sake of a function overload.

Hmm. Valid javascript or not, it's a flawed design. Either Graphics derives from DisplayObject or it does not. If it does, it must provide a working definition of DisplayObject.generateTexture. Typescript is simply enforcing proper design.

I'm not sure what precipitated the original change to the signature of DisplayObject.generateTexture, but you might want to reconsider...

Yeah I am having this issue even when rolling back to 2.4.6. I tried that function overloading recommendation and didn't seem to work. I am also wondering if this has anything to do with the misbehaving of the cursor when changing to a hand when over a graphic.

@smks - i modified the Graphics signature to match that of DisplayObject and I'm working ok...though I'm exercising very little of the engine.

This is kinda a blocker for me right now. @shinygruv3 How did you get it working exactly?

@JimmyBoh - I modified ../phaser/typescript/pixi.d.ts locally and changed the signature of Graphics.generateTexture to match that of DisplayObject.generateTexture:

generateTexture(resolution?: number, scaleMode?: number, renderer?: PixiRenderer): RenderTexture;

Awesome, thanks @shinygruv3. I'm just starting a new project with Phaser. It's small, but I will share any issues or oddities I encounter.

This is now fixed in the dev branch. Well, I say 'fixed', the defs are basically reverted back to how they were before. So the TS compilation errors are gone, but the return type is technically incorrect. Then again, it has been for several years now and no-one complained, so such is life.

@shinygruv3 Your turnaround is not working for me :(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

halilcakar picture halilcakar  ·  4Comments

rootasjey picture rootasjey  ·  3Comments

cncolder picture cncolder  ·  4Comments

hugoruscitti picture hugoruscitti  ·  3Comments

MarkSky picture MarkSky  ·  3Comments