Jump to content

TypeScript/Visual Studio issues with game.input


Recommended Posts

Hi everyone. I'm playing around with TypeScript and Phaser for the first time. I started off with plain Javascript/Phaser in Sublime and got a little game going where I could move a sprite around the screen and click to shoot. It worked relatively easily which was nice.

Then I decided to switch to TypeScript and Visual Studio because intellisense and the class/inheritance syntax looked like it would make things easier. I still have hope, but I've run into some problems.


The first issue that came up was setting up WASD controls with AddKey. I tried to use the recommended

var movementLeftKey: Phaser.Key = game.input.keyboard.addKey(Phaser.KeyCode.A);

but I got the message: Property 'KeyCode' does not exist on 'typeof Phaser'

I got around it by using

var movementLeftKey: Phaser.Key = game.input.keyboard.addKey(Phaser.Keyboard.A)

This made me think that I was using the wrong version of phaser files since 2.4.4 ought to allow KeyCode.

Unless I'm doing something wrong(certainly possible), I believe I'm all up to date. I have p2.d.ts, phaser.d.ts, phaser.js, phaser.min.js, and pixi.comments.d.ts from https://github.com/photonstorm/phaser/tree/master/typescript all in my project as recommended in the typescript tutorial: http://phaser.io/tutorials/how-to-use-phaser-with-typescript

I wanted to use phaser.comments.d.ts instead of phaser.d.ts, but it looks like it hasn't been updated - the version appears to be 2.4.3 rather than 2.4.4


Another issue is with detecting mouse clicks.

Before typescript/visual studio, I used game.input.activePointer.leftButton.isDown to check if the player was clicking. Visual Studio won't even compile that because it says game.input.activePointer.leftButton is a boolean. All of the Phaser documentation says it's a DeviceButton, and I've even printed out the value with console.log and it says it's an [object Object], so definitely not a boolean. Any ideas why Visual Studio thinks otherwise?


I am using the most recent version of typescript that came installed with Visual Studio 2015 Community.

Here's my project on Github. I haven't ever used github before so if something appears messed up with how I uploaded it, that's why and please let me know.

The code dealing with mouse and keyboard input is here.


Thanks in advance for any advice.

Link to comment
Share on other sites

Hi, from time to time you may encounter small errors like that with leftbutton. When something like this happen to me I just adjust phaser.d.ts.


For the keys, I found this in release notes for 2.4.4:

Phaser.KeyCode is a new pseudo-type used by the Keyboard class (and your code) to allow for separation of all the Keyboard constants to their own file. This stops the JSDocs from becoming 'polluted' and allows for easier future expansion (thanks @pnstickne #2118 #2031)


This is bottom of Keyboard.js file:

// Duplicate Phaser.KeyCode values in Phaser.Keyboard for compatibilityfor (var key in Phaser.KeyCode) {    if (Phaser.KeyCode.hasOwnProperty(key) && !key.match(/[a-z]/)) {        Phaser.Keyboard[key] = Phaser.KeyCode[key];    }}

 Looks like Keyboard is old way and KeyCode is new one, but phaser.d.ts was not updated yet...

Link to comment
Share on other sites

Thanks Tom,

I opened up phaser.d.ts and edited the line:

        leftButton: boolean;


        leftButton: Phaser.DeviceButton;

and now it works as expected.


The KeyCode vs Keyboard issue is pretty minor since they both work, but I'll make sure to change it when phaser.d.ts is updated. I'm glad it wasn't something silly that I made a mistake with though.


Thank you for the help and quick response.



Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...