Skip to content

Instantly share code, notes, and snippets.

@ryanwilliams
Created February 18, 2012 11:54
Show Gist options
  • Save ryanwilliams/1858984 to your computer and use it in GitHub Desktop.
Save ryanwilliams/1858984 to your computer and use it in GitHub Desktop.
Cocos2D v0.2 example in CoffeeScript
# Pull in the modules we're going to use
cocos = require 'cocos2d' # Import the cocos2d module
nodes = cocos.nodes # Convenient access to 'nodes'
events = require 'events' # Import the events module
geo = require 'geometry' # Import the geometry module
# Convenient access to some constructors
Layer = nodes.Layer
Scene = nodes.Scene
Label = nodes.Label
Director = cocos.Director
Point = geo.Point
class CoffeeTest extends Layer
constructor: ->
# You must always call the super class constructor
super
# Get size of canvas
s = Director.sharedDirector.winSize
# Create label
label = new Label string: 'Coffee Test'
, fontName: 'Arial'
, fontSize: 67
# Position the label in the centre of the view
label.position = new Point s.width / 2, s.height / 2
# Add label to layer
this.addChild label
# Initialise application
exports.main = ->
# Get director singleton
director = Director.sharedDirector
# Wait for the director to finish preloading our assets
events.addListener director, 'ready', (director) ->
# Create a scene and layer
scene = new Scene
layer = new CoffeeTest
# Add our layer to the scene
scene.addChild layer
# Run the scene
director.replaceScene scene
# Preload our assets
director.runPreloadScene()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment