Arcanis Posted August 24, 2015 Share Posted August 24, 2015 Hello guys, I read http://www.html5gamedevs.com/topic/6495-add-more-custom-properties-to-sprite/ 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 ? Link to comment Share on other sites More sharing options...
Tom Atom Posted August 24, 2015 Share Posted August 24, 2015 Hi, 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) Karthika G 1 Link to comment Share on other sites More sharing options...
jouniii Posted August 25, 2015 Share Posted August 25, 2015 One other correct way would be to use interfaces to describe the new property to allow type checking on this sort of "mixins". Link to comment Share on other sites More sharing options...
Tom Atom Posted August 25, 2015 Share Posted August 25, 2015 @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? Link to comment Share on other sites More sharing options...
Recommended Posts