Skip to content

Instantly share code, notes, and snippets.

@tonyhb
Created October 30, 2012 22:40
Show Gist options
  • Save tonyhb/3983573 to your computer and use it in GitHub Desktop.
Save tonyhb/3983573 to your computer and use it in GitHub Desktop.
Game State
# Contains the game state in a protected (closure) variable
state : ->
# Our protected variable, only changeable through the setState method
# defined below
_state = 'stopped'
# The state method will (from the 2nd call onwards) only return the
# state.
@.state = ->
_state
# Sets the Game state to allowed members only
@.setState = (newState) ->
if ['stopped', 'playing', 'paused'].indexOf(newState) < 0
throw new Error("Unknown state '" + newState.toString() + "'")
else
_state = newState
@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment