initialState = { x: 0, y: 112, sprite: 0, counter: 0, flip: false } const walkCycle = [1, 2, 3, 4, 5, 6] update = (state, input) => { if (input.left) { state.x -= 1 state.flip = true } if (input.right) { state.x += 1 state.flip = false } if (input.left || input.right) { const slowCounter = Math.floor(state.counter / 5) state.sprite = walkCycle[slowCounter % 6] state.counter++ } else { state.sprite = 0 state.counter = 0 } } draw = state => { clear() map() sprite(state.x, state.y, state.sprite, 0, state.flip) }