Sign in to follow this  

how to add my custom Property to Phaser.Sprite (using TypeScript)

Recommended Posts

Hello guys,


I read already and understand that in javascript it's free to add any custom property into any object.


but i just get started Phaser with TypeScript because of I have experience in strong-type language like C#.
I use its type-strict and don't want to use "any" type in order to use intellisense, that make i cannot add custom property to sprite object.


Could you mind to tell me how to solve this problem ?  


Share this post

Link to post
Share on other sites



 there are 2 oprtions:


1) subclass Phaser.Sprite like:

class MySprite extends Phser.Sprite {     private _myCustomProperty: number = 10;}

2) add property dynamically like:

// write: sprite["myCustomProperty"] = 10;// read: console.log(sprite["myCustomProperty"]);

 I use both ... most of times first one if I am also adding methods or more than one property or if I am using new property widely in code. Second if I am adding only one property and using it only on limited places in code (for example: in last game we have map with levels and I added level number in this way to level sprite as only place where I needed it was in on click callback)

Share this post

Link to post
Share on other sites

@jouniii: I understand, that you can add new property by adding it to interface like this:

interface Window {    myString: string;}window.myString = "Hi!";

because Window interface already exists and additional property definition will be merged with it.


But how can I do it with Phaser.Sprite, which is defined as class (not as interface)? Can you, please, give small example?

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.