Sign in to follow this  
Heppell08

Creating Keyboard Keys

Recommended Posts

I'm trying to create and assign a pause to my game. I want it on the letter 'P' but for some reason it wont bind at all. I'm using the same code thats in the examples of the phaser sources and yet still it won't bind at all. 

I've got cursors working with the var cursors and input as create keys and even when i use that approach it wont bind to 'P'.

its set like so:

cursors = game.input.keyboard.createCursorKeys();if(cursors.left.isDown)	{		player.body.velocity.x = -150;        if(facing !== 'left')        {            facing = 'left';        }    }// they all work as standard for up/left and right

and i'm trying to do this in this:

if(cursors.P.justPressed){    game.state.pause;}// not sure if the game will pause, cant bind key...

Keep getting cant read property justPressed of undefined and also cannot read property of undefined P.

I'm using the phaser code provided so i have got a clue what im doing wrong.

I've went through this > http://gametest.mobi/phaser/docs/Phaser.Key.html#toc14

too. Any help? Thanks

Share this post


Link to post
Share on other sites

P is not a cursor key, they are only the arrow keys.

createCursorKeys() is a shortcut function that sets up the arrow keys quickly, as they are commonly used.

 

What you want is:

var pauseKey = game.input.keyboard.addKey(Phaser.Keyboard.P);

Share this post


Link to post
Share on other sites

You should probably use an onDown signal instead of justPressed.

justPressed doesn't tell you if a key was pressed in the last frame, or if it just entered the pressed state, it tells you if it was pressed in the last x milliseconds...

so in the update function, justPressed will return true for multiple updates, effectively pausing and unpausing your game until the key is released, or the threshhold for justPressed is reached.

 

onDown allows you to attach callbacks to when the key is pressed, and they will fire once as the key enters the pressed state.

Something like this:

pauseKey.onDown.add(pauseFunction, this);pauseFunction = function(){    pause = !pause;}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.