Jump to content

Having some trouble with the `keyboard.addCallbacks` code.


christian.tucker
 Share

Recommended Posts

I followed the word input example to try to create my own version of a text-input-field, as it seems like phaser doesn't have one. 

 

Doing so I have the following code

 

Create() 

game.input.keyboard.addCallbacks(this, null, null, this.keyPress);
Then another defined method:

keyPress: function(char) {    console.log("pressed");    if(this.selected == 1) {        this.userFieldText.text += char;    } else if(this.selected == 2) {        this.passFieldText.text += char;    }}
I'm using states, so the full class looks like so:

/** * Created by Jellal on 12/9/2014. */var loginButton;var userField;var userFieldText;var passField;var passFieldText;var selected;var stateLogin = {    preload: function() {        game.stage.backgroundColor = '#000';        game.load.image('text-field', 'assets/ui/text-box.png');        game.load.image('login-button', 'assets/ui/login-button.png');    },    create: function() {        this.selected = 0;        var userLabel = game.add.text(200, 200, 'Enter a username: ');        userLabel.fontSize = 20;        userLabel.fill='#fff';        var passLabel = game.add.text(200, 250, 'Enter a password: ');        passLabel.fontSize = 20;        passLabel.fill='#fff';        var userField = game.add.sprite(350, 195, 'text-field');        userField.inputEnabled = true;        userField.events.onInputUp.add(function() { this.selectField('user') }, this);        var passField = game.add.sprite(350, 245, 'text-field');        passField.inputEnabled = true;        passField.events.onInputUp.add(function() { this.selectField('pass') }, this);        this.userFieldText = game.add.text(360, 200, '');        this.userFieldText.fontSize = 16;        this.userFieldText.fill = "#fff";        this.passFieldText = game.add.text(360, 250, '');        this.passFieldText.fontSize = 16;        this.passFieldText.fill = "#fff";        game.input.keyboard.addCallbacks(this, null, null, this.keyPress);    },    update: function() {        console.log('Selected: ' + this.selected + ' Text: ' + this.userFieldText.text);    },    selectField: function(field) {        if(field == 'user') {            this.selected = 1;        } else if(field == 'pass') {            this.selected = 2;        }    },    keyPress: function(char) {        console.log("pressed");        if(this.selected == 1) {            this.userFieldText.text += char;        } else if(this.selected == 2) {            this.passFieldText.text += char;        }    }};
There are no errors displayed; However it just simply doesn't work. If i remove the 'this' from the call, I then get an undefined error.
Link to comment
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...
 Share

  • Recently Browsing   0 members

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