0

I'm just getting started with Javascript, going on my second day, but I ran into a small issue. I'm using the Phaser API and I'm making use of states, basically my problem is here:

        game.input.keyboard.addCallbacks(this, null, null, this.keyPress);

In which I have the following code in the same state

keyPress: function(char) {
    console.log("pressed");
    if(this.selected == 1) {
        this.userFieldText.text += char;
    } else if(this.selected == 2) {
        this.passFieldText.text += char;
    }
}

There's no errors in the console, but the method is never called.

Documentation here


Full class

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

};
Hobbyist
  • 15,888
  • 9
  • 46
  • 98
  • I don't know what's wrong with your usage of the framework, but if the function would be called [it would not work](http://stackoverflow.com/q/20279484/1048572) indeed. – Bergi Dec 10 '14 at 03:23
  • The link that you have provided doesn't really make any sense in this scenario, where the use of 'this' is required and even used in the demonstrations to get certain functionality to work. If you do not use 'this' you will be flooded with undefined context errors. – Hobbyist Dec 10 '14 at 03:26
  • You need to tell phaser about your `preload` and `create`. Can you show us that code? I suspect it is not setting your `stateLogin ` as this when calling them – AlexanderBrevig Dec 10 '14 at 03:34
  • @Alexander -- `game.state.add('stateCity', stateCity); game.state.add('stateArena', stateArena); game.state.add('stateLogin', stateLogin); game.state.start('stateLogin');` My states are being loaded properly, I'm only having problems with this single callback. Everythings drawing correctly, and my other states work fine too. I have all of my scripts seperated and organized. – Hobbyist Dec 10 '14 at 03:44

0 Answers0