Jump to content

game variable unidentified inside create function


Recommended Posts

Hi guys, I can't seem to access my game variable from the create function of the same script. 

*How can I get a ref to it? I need to add a callback for the keyboard inputs.

The call to sign up the callback throws an exception:

Uncaught TypeError: Cannot set property 'onPressCallback' of undefined

If someone can assist me in understanding how to get the game object it would be great,


var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 300 },
            debug: false
    scene: {
        preload: preload,
        create: create,
        update: update

var game = new Phaser.Game(config);

function create ()
    // not calling the function
    game.input.keyboard.onPressCallback = function () {console.log("Hello World")};

    // returns null


Link to comment
Share on other sites

There isn't a single system in v3 that needs to be accessed via 'game'. Keyboard, especially, is one of them.

Access it via `this.input.keyboard` from a Scene, although there's no such thing as an 'onPressCallback' either, so it still won't actually do anything even with the correct reference.

Link to comment
Share on other sites

//put this in create
this.keyBools = {
            up: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W),
            down: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S),
            left: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A),
            right: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D),
            shift: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SHIFT),
            pause: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ESC)

//in update

    // do the things

you don't have to put them in an object, but's neater.


-- edit:

sorry, I realise that isn't helpful, you don't want to poll i take it. try and find emit in the docs.
or maybe this is the thing - https://photonstorm.github.io/phaser3-docs/Phaser.Input.InputManager.html#addDownCallback

Link to comment
Share on other sites


  • Recently Browsing   0 members

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