One thing I hadn't thought about was the behaviour of listening to the P key to pause and also resume. The first iteration of the code would flicker madly between the game page and the paused page whenever the P key was pressed - the game loop runs so fast that the app would transition multiple times between the two pages, because the isDown test would simply toggle the state of this.state.paused. I messed around for a while with storing the UNIX time when the key was first pressed, and comparing it to the current time, before realising I really just needed to know what "direction" was being requested, and then waiting for this "request" to finish before completing the transition.
...
debounce = (testCondition, key, newState) => {
if (testCondition) {
if (this.keyListener.isDown(key)) {
// No. They are still holding it
return true;
}
this.setState(newState);
}
return false;
};
handleKeysWhilePaused = () => {
if (this.debounce(this.state.pauseRequested, P, {
pauseRequested: false,
paused: true
})) {
return;
}
if (this.debounce(this.state.unpauseRequested,P, {
unpauseRequested: false,
paused: false
})) {
return;
}
if (this.keyListener.isDown(Q)) {
this.props.onGameExit();
}
if (this.keyListener.isDown(P)) {
this.setState({unpauseRequested: true});
}
}
handleGameplayKeys = () => {
if (this.keyListener.isDown(P)) {
this.setState({pauseRequested: true});
}
}
update = () => {
if (this.state.pauseRequested || this.state.paused) {
this.handleKeysWhilePaused();
} else {
this.handleGameplayKeys();
}
};
...
Effectively I've implemented an isUp(), which is surprisingly not in the react-game-kit library. Oh well, it's all good learning.



No comments:
Post a Comment
Comments welcome - spam is not. Spam will be detected, deleted and the source IP blocked.