max-m Posted April 13, 2014 Share Posted April 13, 2014 Hi folks, is there a way to preserve input bindings on state changes? As far as I know, Phaser copies the following members from the current to the new state:game, game.add, game.make, game.camera,game.cache, game.input, game.load, game.math,game.sound, game.scale, this, game.stage,game.time, game.tweens, game.world, game.particles,game.rnd, game.physics'game.input' is one of them. I've built a small JSFiddle to test the behavior, press Space to update, Enter to start state 2.It logs it's output to the console. Link to comment Share on other sites More sharing options...
rich Posted April 14, 2014 Share Posted April 14, 2014 It doesn't copy them when you change state, all it does is create a new reference. It's still the same sub-systems underneath. However during a state change Input.reset is called, which clears down any bindings that may exist to in-game objects. It's most likely this action you want to avoid? Link to comment Share on other sites More sharing options...
max-m Posted April 14, 2014 Author Share Posted April 14, 2014 Hi, yes, it seems to be that action.I just want to preserve some input bindings that are of use in all states (currently for debugging). Monkey patching doesn't seem to be usable here (or here), should I instead create a global (in game) function containing the bind-logic and call it in my states during 'init' or 'create'? Here's some CoffeeScript of my "global" binds, should be readable even you're not familiar with CoffeeScript - or you could look at the compiled Source here ^^.game.input.keyboard.addKey(Phaser.Keyboard.F1).onUp.add -> # reset game game.state.start 'BootState', @game.input.keyboard.addKey(Phaser.Keyboard.F2).onUp.add -> console.log 'Resetting saves' # some more code, @game.input.onDown.add -> game.scale.startFullScreen no, @# Fix fullscreengame.scale.enterFullScreen.add -> game.parentStyles = document.getElementById(game.parent).getAttribute 'style', @# Fix fullscreengame.scale.leaveFullScreen.add -> document.getElementById(game.parent).setAttribute 'style', game.parentStyles do game.scale.refresh, @I had to fix the fullscreen mode by that, because I use the 'game.parent' element as target and got some custom CSS to get Firefox to scale the game I want it (not streched fullscreen ) Link to comment Share on other sites More sharing options...
Pedro Alpera Posted April 14, 2014 Share Posted April 14, 2014 I'm trying to resolve a similar issue, is it normal that Input.reset is called when you lost focus or change tab in the browser? We have been talking about that here http://www.html5gamedevs.com/topic/5611-203-possible-keyboard-input-bug/I suppose that your solution could work for both problems. Link to comment Share on other sites More sharing options...
max-m Posted April 14, 2014 Author Share Posted April 14, 2014 Hello Pedro, while looking for the call of Phaser.Input.reset I also came across this and this.This could explain why my key bindings sometimes "seemed to not work", right? Link to comment Share on other sites More sharing options...
rich Posted April 14, 2014 Share Posted April 14, 2014 I'm trying to resolve a similar issue, is it normal that Input.reset is called when you lost focus or change tab in the browser? We have been talking about that here http://www.html5gamedevs.com/topic/5611-203-possible-keyboard-input-bug/I suppose that your solution could work for both problems. Yes it's normal. Without it keys and pointers can get 'stuck' in a down state and things can get very weird. However, see the latest commit just made to the dev branch: https://github.com/photonstorm/phaser/commit/a7f6165e3901985c099861da02cc4b86e7c22958 Link to comment Share on other sites More sharing options...
Pedro Alpera Posted April 15, 2014 Share Posted April 15, 2014 Thanks Max-m and Rich I have tried the dev branch and works like a charm. Link to comment Share on other sites More sharing options...
max-m Posted April 15, 2014 Author Share Posted April 15, 2014 Hi Richard, Yes it's normal. Without it keys and pointers can get 'stuck' in a down state and things can get very weird. However, see the latest commit just made to the dev branch: https://github.com/photonstorm/phaser/commit/a7f6165e3901985c099861da02cc4b86e7c22958 All calls made to InputManager.reset, such as from a State change, are ignored. So now the bindings persist between states and when adding an handler one should unbind the previous handler, right? Link to comment Share on other sites More sharing options...
Pedro Alpera Posted April 15, 2014 Share Posted April 15, 2014 EDIT POST: I have deleted my post to avoid confusion Link to comment Share on other sites More sharing options...
rich Posted April 15, 2014 Share Posted April 15, 2014 So now the bindings persist between states and when adding an handler one should unbind the previous handler, right? No, not between State. That still does a full Input reset. What doesn't do a full reset, which used to, is window focus loss. However what I added was this:InputManager.resetLocked - If the Input Manager has been reset locked then all calls made to InputManager.reset, such as from a State change, are ignored. max-m and Lord Dan 2 Link to comment Share on other sites More sharing options...
max-m Posted April 15, 2014 Author Share Posted April 15, 2014 Ah, ok, I see. So I could set this variable to true just before I switch the state and revert it afterwards. Certain bindings could then be removed in the shutdown function of my states, right? Link to comment Share on other sites More sharing options...
rich Posted April 16, 2014 Share Posted April 16, 2014 Yup. Or just set it and forget about it on Boot and always remember to destroy, it's up to you really. Link to comment Share on other sites More sharing options...
max-m Posted April 16, 2014 Author Share Posted April 16, 2014 Thanks to both of you I guess now I'll try to finish up my main menu / save selection and creation screens (this is a nice little exercise to get closer with Phaser ^^) and try to come up with ideas of handling the world (the Tiled-maps should be fine, just need to take a look at locking the camera so only one “screen” or “frame” is visible at a time).Btw. I'm writing an TLoZ (NES) clone (my first game after an Pong game in Java I wrote 2 years or so ago), 256px by 240px, original art style I'll ask here in the forums if I'm stuck at something Have a nice day.Maximilian Link to comment Share on other sites More sharing options...
Recommended Posts