updatestage Posted April 13, 2014 Share Posted April 13, 2014 Hi there, I want to add support for "external cursor keys" which means that if the game runs on mobile device I want to use Buttons (e.g. HTML Elements) which should have the same functionality as the cursor keys. I'm working with TypeScript. Is it possible to extend Phaser.Keyboard and replace the "game.input.keyboard" property by that (with all its current properties)? Or is there a better way to solve this? Link to comment Share on other sites More sharing options...
rich Posted April 14, 2014 Share Posted April 14, 2014 Why not just create your Buttons via normal JavaScript, and in their down events do whatever you need to inside Phaser? (move your player, whatever). Link to comment Share on other sites More sharing options...
codevinsky Posted April 14, 2014 Share Posted April 14, 2014 Or, just use Phaser.Button? Link to comment Share on other sites More sharing options...
updatestage Posted April 15, 2014 Author Share Posted April 15, 2014 Thanks for your replies! But I wanted a reusable solution with minimized dependencies. I've done it this way:module Tools{ export class Input{ static CURSOR_DOWN:number = 0; static CURSOR_UP:number = 1; static CURSOR_LEFT:number = 2; static CURSOR_RIGHT:number = 3; static cursors; static keys = [false,false,false,false]; static init(cursors){ Input.cursors = cursors; } static isDown(key){ switch (key) { case Input.CURSOR_DOWN: { return Input.keys[key] || Input.cursors.down.isDown; break; } case Input.CURSOR_UP: { return Input.keys[key] || Input.cursors.up.isDown; break; } case Input.CURSOR_LEFT: { return Input.keys[key] || Input.cursors.left.isDown; break; } case Input.CURSOR_RIGHT: { return Input.keys[key] || Input.cursors.right.isDown; break; } default: { // TODO: Implemente default case console.log("default case"); } } } static keyDown(key){ Input.keys[key] = true; } static keyUp(key){ Input.keys[key] = false; } static pressedKeys(){ return { down: Input.isDown(Input.CURSOR_DOWN), up: Input.isDown(Input.CURSOR_UP), left: Input.isDown(Input.CURSOR_LEFT), right: Input.isDown(Input.CURSOR_RIGHT) }; } }}So I can use it like that:<!-- HTML --><a onTouchStart="Tools.Input.keyDown(0)" onTouchEnd="Tools.Input.keyUp(0)">↓</a><a onTouchStart="Tools.Input.keyDown(1)" onTouchEnd="Tools.Input.keyUp(1)">↑</a><a onTouchStart="Tools.Input.keyDown(2)" onTouchEnd="Tools.Input.keyUp(2)">←</a><a onTouchStart="Tools.Input.keyDown(3)" onTouchEnd="Tools.Input.keyUp(3)">→</a>// Typescriptcreate(){ Tools.Input.init(this.game.input.keyboard.createCursorKeys());}update(){ // get all cursor keys var keys_pressed = Tools.Input.pressedKeys(); if (keys_pressed.up) ... // get a single one if (Tools.Input.isDown(Tools.Input.CURSOR_DOWN)) ...}Seems to work in the first testings ;-) adamyall 1 Link to comment Share on other sites More sharing options...
Recommended Posts