Skip to content

Instantly share code, notes, and snippets.

@cha0s
Created October 1, 2012 13:17
Show Gist options
  • Save cha0s/3811739 to your computer and use it in GitHub Desktop.
Save cha0s/3811739 to your computer and use it in GitHub Desktop.
Avocado example State
# Avocado loads the 'Initial' state, and from there it's all up to you!
avo.States['Initial'] = new class
# Called the first time this state is loaded. You can set up any stuff your
# state needs here.
constructor: ->
# Yum, an avocado!
@avocado = {}
# Let's move around the avocado! In order to do that, we'll need to
# keep track of its x, y location.
@x = 0
@y = 0
# Called every time this state is loaded. You should do things like loading
# images and setting up your event handlershere.
initialize: (args) ->
# Load that avocado image.
avo.Image.load('/image/avocado.png').then (image) => @avocado = image
# Handle user input events.
avo.input.on 'inputEvent.State', (type) =>
switch type
# Up subtracts 1 from y.
when 'moveUp' then @y--
# Right adds 1 to x.
when 'moveRight' then @x++
# Down adds 1 to y.
when 'moveDown' then @y++
# Left subtracts 1 from x.
when 'moveLeft' then @x--
# Called repeatedly while this state is loaded. You can do things like
# update your world here.
tick: ->
# Called repeatedly while this state is loaded. You can render all of
# your pretty pictures here!
render: (buffer) ->
# Fill the screen with white.
buffer.fill 255, 255, 255
# Show the avocado at its current x, y location.
@avocado.render [@x, @y], buffer
# Called when another state is loaded. This gives you a chance to clean
# up resources and event handlers.
onExit: (nextStateName) ->
# Remove our user input event handler.
input.off 'inputEvent.State'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment