JavaScript
var game = {
"onload" : function onload() {
if (!me.video.init("screen", 640, 480)) {
alert("Your browser does not support HTML5 canvas.");
return;
}
// Initialize Chipmunk-js
var space = cm.getSpace();
space.gravity = cp.v(0, -500);
// melonJS configuration
me.sys.pauseOnBlur = false;
// Set the "Play" ScreenObject.
me.state.set(me.state.PLAY, new game.PlayScreen(true));
// Initialize melonJS and display a loading screen.
me.state.change(me.state.PLAY);
}
};
game.PlayScreen = me.ScreenObject.extend({
"onResetEvent" : function onResetEvent() {
// Create a floor
var h = me.video.getHeight() / 2;
var space = cm.getSpace();
var floor = space.addShape(new cp.SegmentShape(
space.staticBody,
cp.v(0, h),
cp.v(me.video.getWidth(), h),
0
));
floor.setElasticity(1);
floor.setFriction(1);
var c1, c2, c3, c4;
// Create left circles
c1 = new game.Circle(100, 50, 10, "#0cf", 0, createStaticBody(), 1);
c2 = new game.Circle(150, 100, 5, "#f22", 0, null, 2, 1);
space.addConstraint(new cp.SlideJoint(c1.body, c2.body, cp.vzero, cp.vzero, 0, 100));
me.game.add(c1, 1000);
me.game.add(c2, 1000);
// Create right circles
c3 = new game.Circle(200, 50, 10, "#fc0", 0, createStaticBody(), 1);
c4 = new game.Circle(250, 100, 5, "#2f2", 0, null, 2, 1);
space.addConstraint(new cp.SlideJoint(c3.body, c4.body, cp.vzero, cp.vzero, 0, 100));
me.game.add(c3, 1000);
me.game.add(c4, 1000);
// Create a damped spring between the control points
space.addConstraint(new cp.DampedSpring(c2.body, c4.body, cp.vzero, cp.vzero, 0, 10, 10));
// Create rope
me.game.add(new game.Rope(c1.body, c2.body, c4.body, c3.body), 1001);
...