Voxin Posted July 24, 2018 Share Posted July 24, 2018 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, Thanks. 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 console.log(this.game); } Link to comment Share on other sites More sharing options...
rich Posted July 24, 2018 Share Posted July 24, 2018 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 More sharing options...
NoxBrutalis Posted July 24, 2018 Share Posted July 24, 2018 //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 if(this.keyBools.up.isDown) { // 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 More sharing options...
NoxBrutalis Posted July 24, 2018 Share Posted July 24, 2018 or this seems more proper as the ondown callbacks all say they aren't really for general purpose input. This is the other thing i mentioned, not much info there tho: https://photonstorm.github.io/phaser3-docs/Phaser.Events.EventEmitter.html#emit I think this is how you can emit custom events, but i dont know how it's set up. Link to comment Share on other sites More sharing options...
rich Posted July 24, 2018 Share Posted July 24, 2018 Or, if you prefer less typing: this.keyBools = this.input.keyboard.addKeys('W,S,A,D'); There are lots of examples here too, for key events, key combos, etc: http://labs.phaser.io/index.html?dir=input/keyboard/&q= NoxBrutalis 1 Link to comment Share on other sites More sharing options...
NoxBrutalis Posted July 24, 2018 Share Posted July 24, 2018 Oh that's sweetly concise, Rich Link to comment Share on other sites More sharing options...
Recommended Posts