phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
36.94k stars 7.08k forks source link

prototype.constructor not set on framework classes #283

Closed darkoverlordofdata closed 10 years ago

darkoverlordofdata commented 10 years ago

I loved the typescript tutorial, and quickly set about converting it to coffeescript. In doing so,I found that the prototype constructor of the framework classes is not set - it still points to Object.

Fixing this touches just about every class in your framework, but I can work around it in coffeescript by patching at the beginning of each class definition, like this:

Phaser.Game::constructor = Phaser.Game module.exports = class Game extends Phaser.Game

The full code is at https://github.com/darkoverlordofdata/phaser-coffeescript-demo

Typescript seems to resolve this at compile time, but Coffee-Script, along with several javascript oop strategies (http://ejohn.org/blog/simple-javascript-inheritance/, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript) expect this property to be set.

This can also break the javascript instanceof operator - x = new Phaser.Game(...); x instanceof Phaser.Game will be false.

Thanks, Bruce

photonstorm commented 10 years ago

That's a fair point and thanks for raising it. We set the constructor on all derived classes of course (like Text or Sprite) but not on the core ones. I'll ensure this is addressed for the 1.1.4 release.

darkoverlordofdata commented 10 years ago

Thanks - I'm looking forward to it!

darkoverlordofdata commented 10 years ago

I just downloaded v 2.0.2 and gave it a spin. I was able to remove my patches:

Phaser.::constructor = Phaser.

and my test app works!

Thank You!!!