Skip to content

Commit 8c44838

Browse files
committed
Update highscore on each iteration
1 parent 1e8f6f7 commit 8c44838

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

pages/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class Page extends React.Component {
187187
}
188188
l.forEach((d, i) => {
189189
keep[d.x][d.y] = false
190-
this.state.score += ((newColoredDot.type + 1) * 5)
190+
this.updateScore(this.state.score + ((newColoredDot.type + 1) * 5))
191191
})
192192
this.state.blocksKilled++
193193
}
@@ -222,17 +222,18 @@ class Page extends React.Component {
222222
dots: [],
223223
floatingDots: []
224224
})
225+
this.updateScore(this.state.score)
225226
}
226227

227-
setHighScore () {
228+
updateScore (score) {
228229
if (typeof window === 'undefined') return
229-
const localStorage = window.localStorage
230-
if (!localStorage) return
231-
localStorage['score'] = Math.max(this.state.score, this.state.highScore)
230+
const highScore = Math.max(score, this.state.highScore)
231+
window.localStorage['score'] = highScore
232+
this.setState({highScore, score})
232233
}
233234

234235
getHighScore () {
235-
if (typeof window === 'undefined') return
236+
if (typeof window === 'undefined') return 0
236237
const localStorage = window.localStorage
237238
if (!localStorage) return
238239
return localStorage['score'] || 0

0 commit comments

Comments
 (0)