Permalink
Browse files

Updated to Cocos2D JavaScript v0.2

  • Loading branch information...
ryanwilliams committed Feb 10, 2012
1 parent 6ee6494 commit e49cd0edac0cd7247a85e28f4ccacc34a0cf9c94
Showing with 186 additions and 160 deletions.
  1. +0 −3 .gitmodules
  2. +2 −0 .npmignore
  3. +3 −0 jah.json
  4. +0 −11 make.json
  5. +19 −0 package.json
  6. +19 −17 public/{index.html → index.html.template}
  7. +61 −59 src/Ball.js
  8. +15 −15 src/Bat.js
  9. +67 −55 src/main.js
View
@@ -1,3 +0,0 @@
-[submodule "cocos2d"]
- path = cocos2d
- url = git://github.com/ryanwilliams/cocos2d-javascript.git
View
@@ -0,0 +1,2 @@
+node_modules/
+build/
View
@@ -0,0 +1,3 @@
+{ "jahProject": { "version": "0.2.0" }
+, "libs": ["cocos2d"]
+}
View
@@ -1,11 +0,0 @@
-
-{
- "output": "public/breakout.js",
- "extensions": ["js", "gif", "jpeg", "jpg", "png", "tmx", "tsx", "plist"],
- "main_module": "main",
-
- "paths": {
- "cocos2d/src" : "/",
- "src" : "/"
- }
-}
View
@@ -0,0 +1,19 @@
+{
+ "name": "Breakout",
+ "author": "",
+ "description": "",
+ "version": "0.0.1",
+ "homepage": "",
+
+ "engines": {
+ "node": ">= 0.4.0"
+ },
+
+ "repository": {
+ },
+
+ "dependencies": {
+ "jah": "~0.2.0",
+ "cocos2d": "~0.2.0-beta3"
+ }
+}
@@ -1,11 +1,9 @@
-
-<!doctype html>
-<html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <script src="breakout.js" type="text/javascript"></script>
- <title>breakout</title>
- <style type="text/css" media="screen">
+<!doctype html>
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <title>Breakout</title>
+ <style type="text/css" media="screen">
body {
font-family: Helvetica, Arial, sans-serif;
font-size: 10pt;
@@ -15,20 +13,24 @@
text-align: center;
text-shadow: 0 2px 1px #fff;
}
- #breakout-demo {
+ .cocos2d-app {
border: 1px solid #000;
width: 480px;
height: 320px;
+ display: block;
margin: 0 auto;
-webkit-box-shadow: 0 3px 10px rgba(0, 0, 0, 0.75);
-moz-box-shadow: 0 3px 10px rgba(0, 0, 0, 0.75);
-o-box-shadow: 0 3px 10px rgba(0, 0, 0, 0.75);
- box-shadow: 0 2px 2px rgba(0, 0, 0, 0.75);
+ box-shadow: 0 3px 10px rgba(0, 0, 0, 0.75);
}
- </style>
- </head>
- <body>
- <h1>breakout</h1>
- <div id="breakout-demo"></div>
- </body>
-</html>
+ </style>
+ </head>
+ <body>
+ <h1>Breakout</h1>
+ <div class="cocos2d-app">
+ Loading Breakout...
+ ${scripts}
+ </div>
+ </body>
+</html>
View
@@ -1,151 +1,153 @@
var cocos = require('cocos2d'),
geom = require('geometry'),
- util = require('util');
+ util = require('util')
-var Ball = cocos.nodes.Node.extend({
- velocity: null,
-
- init: function() {
- Ball.superclass.init.call(this);
+function Ball () {
+ Ball.superclass.constructor.call(this)
- var sprite = cocos.nodes.Sprite.create({
- file: '/resources/sprites.png',
- rect: new geom.Rect(64, 0, 16, 16)
- });
+ var sprite = new cocos.nodes.Sprite({
+ file: '/resources/sprites.png',
+ rect: new geom.Rect(64, 0, 16, 16)
+ })
- sprite.set('anchorPoint', new geom.Point(0, 0));
- this.addChild({child: sprite});
- this.set('contentSize', sprite.get('contentSize'));
+ sprite.anchorPoint = new geom.Point(0, 0)
+ this.addChild({child: sprite})
+ this.contentSize = sprite.contentSize
- this.set('velocity', new geom.Point(60, 120));
- this.scheduleUpdate();
+ this.velocity = new geom.Point(60, 120)
+ this.scheduleUpdate()
+}
- },
+Ball.inherit(cocos.nodes.Node, {
+ velocity: null,
- update: function(dt) {
- var pos = util.copy(this.get('position')),
- vel = util.copy(this.get('velocity'));
+ update: function (dt) {
+ var pos = util.copy(this.position),
+ vel = util.copy(this.velocity)
// Test X position
if (!this.testBlockCollision('x', dt * vel.x)) {
// Adjust X position
- pos.x += dt * vel.x;
- this.set('position', pos);
+ pos.x += dt * vel.x
+ this.position = pos
}
// Test Y position
- if (!this.testBlockCollision('y', dt * vel.y)) {
+ if (!this.testBlockCollision('y', -dt * vel.y)) {
// Adjust Y position
- pos.y += dt * vel.y;
- this.set('position', pos);
+ pos.y -= dt * vel.y
+ this.position = pos
}
// Test Edges and bat
- this.testBatCollision();
- this.testEdgeCollision();
+ this.testBatCollision()
+ this.testEdgeCollision()
},
- testBatCollision: function() {
- var vel = util.copy(this.get('velocity')),
- ballBox = this.get('boundingBox'),
+ testBatCollision: function () {
+ var vel = util.copy(this.velocity),
+ ballBox = this.boundingBox,
// The parent of the ball is the Breakout Layer, which has a 'bat'
// property pointing to the player's bat.
- batBox = this.get('parent').get('bat').get('boundingBox');
+ batBox = this.parent.bat.boundingBox
// If moving down then check for collision with the bat
if (vel.y > 0) {
if (geom.rectOverlapsRect(ballBox, batBox)) {
// Flip Y velocity
- vel.y *= -1;
+ vel.y *= -1
}
}
// Update position and velocity on the ball
- this.set('velocity', vel);
+ this.velocity = vel
},
- testEdgeCollision: function() {
- var vel = util.copy(this.get('velocity')),
- ballBox = this.get('boundingBox'),
+ testEdgeCollision: function () {
+ var vel = util.copy(this.velocity),
+ ballBox = this.boundingBox,
// Get size of canvas
- winSize = cocos.Director.get('sharedDirector').get('winSize');
+ winSize = cocos.Director.sharedDirector.winSize
// Moving left and hit left edge
if (vel.x < 0 && geom.rectGetMinX(ballBox) < 0) {
// Flip Y velocity
- vel.x *= -1;
+ vel.x *= -1
}
// Moving right and hit right edge
if (vel.x > 0 && geom.rectGetMaxX(ballBox) > winSize.width) {
// Flip X velocity
- vel.x *= -1;
+ vel.x *= -1
}
// Moving up and hit top edge
- if (vel.y < 0 && geom.rectGetMinY(ballBox) < 0) {
+ if (vel.y < 0 && geom.rectGetMaxY(ballBox) > winSize.height) {
// Flip X velocity
- vel.y *= -1;
+ vel.y *= -1
}
// Moving down and hit bottom edge - DEATH
- if (vel.y > 0 && geom.rectGetMinY(ballBox) > winSize.height) {
+ if (vel.y > 0 && geom.rectGetMaxY(ballBox) < 0) {
// Restart game
- this.get('parent').restart();
+ this.parent.restart()
}
- this.set('velocity', vel);
+ this.velocity = vel
},
- testBlockCollision: function(axis, dist) {
- var vel = util.copy(this.get('velocity')),
- box = this.get('boundingBox'),
+ testBlockCollision: function (axis, dist) {
+ var vel = util.copy(this.velocity),
+ box = this.boundingBox,
// A map is made of mulitple layers, but we only have 1.
- mapLayer = this.get('parent').get('map').get('children')[0];
+ mapLayer = this.parent.map.children[0]
+
+ // Get size of canvas
+ var s = cocos.Director.sharedDirector.winSize
// Add the amount we're going to move onto the box
- box.origin[axis] += dist;
+ box.origin[axis] += dist
// Record which blocks were hit
- var hitBlocks = [];
+ var hitBlocks = []
// We will test each corner of the ball for a hit
var testPoints = {
nw: util.copy(box.origin),
sw: new geom.Point(box.origin.x, box.origin.y + box.size.height),
ne: new geom.Point(box.origin.x + box.size.width, box.origin.y),
se: new geom.Point(box.origin.x + box.size.width, box.origin.y + box.size.height)
- };
+ }
for (var corner in testPoints) {
- var point = testPoints[corner];
+ var point = testPoints[corner]
// All our blocks are 32x16 pixels
var tileX = Math.floor(point.x / 32),
- tileY = Math.floor(point.y / 16),
- tilePos = new geom.Point(tileX, tileY);
+ tileY = Math.floor((s.height - point.y) / 16),
+ tilePos = new geom.Point(tileX, tileY)
// Tile ID 0 is an empty tile, everything else is a hit
if (mapLayer.tileGID(tilePos) > 0) {
- hitBlocks.push(tilePos);
+ hitBlocks.push(tilePos)
}
}
// If we hit something, swap directions
if (hitBlocks.length > 0) {
- vel[axis] *= -1;
+ vel[axis] *= -1
}
- this.set('velocity', vel);
+ this.velocity = vel
// Remove the blocks we hit
for (var i=0; i<hitBlocks.length; i++) {
- mapLayer.removeTile(hitBlocks[i]);
+ mapLayer.removeTile(hitBlocks[i])
}
return (hitBlocks.length > 0)
}
-});
+})
-module.exports = Ball;
+module.exports = Ball
View
@@ -1,19 +1,19 @@
-var cocos = require('cocos2d'),
- geom = require('geometry');
+var cocos = require('cocos2d')
+ , geom = require('geometry')
-var Bat = cocos.nodes.Node.extend({
- init: function() {
- Bat.superclass.init.call(this);
+function Bat () {
+ Bat.superclass.constructor.call(this)
- var sprite = cocos.nodes.Sprite.create({
- file: '/resources/sprites.png',
- rect: new geom.Rect(0, 0, 64, 16)
- });
+ var sprite = new cocos.nodes.Sprite({
+ file: '/resources/sprites.png',
+ rect: new geom.Rect(0, 0, 64, 16)
+ })
- sprite.set('anchorPoint', new geom.Point(0, 0));
- this.addChild({child: sprite});
- this.set('contentSize', sprite.get('contentSize'));
- }
-});
+ sprite.anchorPoint = new geom.Point(0, 0)
+ this.addChild({child: sprite})
+ this.contentSize = sprite.contentSize
+}
-module.exports = Bat;
+Bat.inherit(cocos.nodes.Node)
+
+module.exports = Bat
Oops, something went wrong.

0 comments on commit e49cd0e

Please sign in to comment.