updatestage

Simulate key events

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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 ;-)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.