Sign in to follow this  
algut20

How to read the Documentation

Recommended Posts

Hello guys,

 

I'm having a bit of trouble applying what I read from the documentation. For instance, I want to use the input keyboard properties of "lastChar" and "lastKey", but I just don't know how to translate that into code. This is what i have tried:

 

  this.game.input.keyboard.lastChar;

  this.game.input.keyboard.lastKey;

  this.game.input.keyboard.lastKey();

 

I tried other combinations that didn't work. Do any of you guys know how to apply the code from the docs that are not seen in any of the examples?

 

Thanks guys!

Share this post


Link to post
Share on other sites

In the Phaser documentation, there are only two kinds of things listed: methods and properties. Properties are just variables and are accessed directly by name. The methods may be invoked with parentheses, with parameters where it applies.

 

If you're having trouble with the "via" column, that refers to the objects pre-defined by Phaser that you can access from a state. When in doubt, they're all accessible from a reference to "game", which is nearly always passed to every object Phaser creates.

 

So this.game.input.keyboard.lastChar; should work,assuming that "this" has a reference to "game". Can you give an example where the code fails? Which part fails?

Share this post


Link to post
Share on other sites

Hey, I figured I was doing something wrong. Well I was testing the code like this:

 

alert(this.game.input.keyboard.lastChar);

 

and it was giving me an undefined. So i didn't know what string value it was actually returning. My goal is to get the lastChar or LasKey to match either the "left" or "right" key arrows and execute code. Like this:

 

if (this.game.input.keyboard.lastChar === 'left')

{ Execute code}

 

But of course that doesnt work, so I'm trying to figure out what exact values are stored in lastChar and LastKey. Thanks for the help!

Share this post


Link to post
Share on other sites

Hello,

 

works for me, perhaps you can provide minimum of your code to show how you tried to implement it?

 

Working example in jsfiddle (don't forget to open your console to see the log).

Don't forget that lastKey keeps storeduntil you press another one, therefore code such as this:

update: function() {        if (this.game.input.keyboard.lastChar === 'left')        {                // produces result on every update until other key is pressed or lastChar reset        }}

Not entirely sure if setting your function off every update loop is your intention, so just pointing it out.

 

 

Either way, I don't know what you do but you can do similar stuff this way:

create: function() {                // add cursor keys        this.cursor = this.input.keyboard.createCursorKeys();        // catch cursor keys' default actions in browser        this.input.keyboard.addKeyCapture([Phaser.Keyboard.UP, Phaser.Keyboard.DOWN,                                           Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT]);},update: function() {       // handle cursor events       if (this.cursor.left.isDown)       {              console.log('do some stuff on left arrow press');       }       // and so on}

Share this post


Link to post
Share on other sites

Hey, Thanks. I looked at the jsfiddle and played around with the developer tools (since I'm still new at this). I'm still not sure i'm getting this. I'm still getting errors and undefines. What I'm basically trying to do is this:

 

 

update: function() {

              if (this.game.input.keyboard.lastChar === 'left')

                  { this.player.frame = 5;}

}

 

I'll keep playing around with this. If you can  give anymore hints, that'd be awesome!

Share this post


Link to post
Share on other sites

Well this is kinda tricky.

 

I checked the Phaser source code around lastChar and found out that it uses String.fromCharCode(string) method. Arrow keys and all other special keys are not printable this way, so you won't get 'left'. I didn't check the entire Phaser, so I don't know how exactly it handles this issue but from checking different inputs I guess that you get an object for letters and numbers which contains lastChar property (as mentioned above non-special characters are printable) with a string representing the character, while in case of special characters you get null, thus javascript engine can't find lastChar property because you are giving it this: "null.lastProperty" which leads to error in javascript execution (can't find property on null object, it has no properties at all).

 

You can try it here, slightly adjusted earlier example in fiddle. Open the console and check what you get if you press "a", "b", "3" on your keyboard and then press any arrow key or shift. You'll find out that it works as long as you don't hit the special keys (except spacebar, keyboard.lastChar returns on spacebar an empty string - that's hardcoded in lastChar method, you can check it in docs over here - in web docs there is always a link to source code right for every property and method mentioned there, I highly recommend checking it sometimes ;-)).

 

So your issues should be probly best handled as I mentioned before:

create: function() {             // add cursor keys        this.cursor = this.input.keyboard.createCursorKeys();      // catch cursor keys' default actions in browser        this.input.keyboard.addKeyCapture([Phaser.Keyboard.UP, Phaser.Keyboard.DOWN,                                           Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT]);},update: function() {       // handle cursor events       if (this.cursor.left.isDown)       {              console.log('do some stuff on left arrow press');       }      // and so on}

It does exactly what you want with Phaser build in props so why don't you actually use it, is there some problem with handling it this traditional way?

Otherwise you can set your own eventListener and check for keyCodes, or use lastKey and check it there (after all phaser already uses eventListenere on your input).

 

Was it helpful or I still don't understand your question?

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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.