Skip to content

Commit

Permalink
added composite events example to Demo.events
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jul 12, 2014
1 parent 2841522 commit 296d47e
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions demo/js/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,32 +994,17 @@
var _world = _engine.world;

Demo.reset();

var stack = Composites.stack(50, 100, 8, 4, 50, 50, function(x, y, column, row) {
return Bodies.circle(x, y, 15, { restitution: 1, render: { strokeStyle: '#777' } });
});

World.add(_world, stack);

var renderOptions = _engine.render.options;
renderOptions.wireframes = false;

var shakeScene = function(engine) {
var bodies = Composite.allBodies(engine.world);
// bind events (_sceneEvents is only used for this demo)

for (var i = 0; i < bodies.length; i++) {
var body = bodies[i];
_sceneEvents.push(

if (!body.isStatic && body.position.y >= 500) {
var forceMagnitude = 0.01 * body.mass;
// an example of using composite events on the world
Events.on(_world, 'afterAdd', function(event) {
console.log('added to world:', event.object);
})

Body.applyForce(body, { x: 0, y: 0 }, {
x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]),
y: -forceMagnitude + Common.random() * -forceMagnitude
});
}
}
};
);

_sceneEvents.push(

Expand Down Expand Up @@ -1084,7 +1069,7 @@

_sceneEvents.push(

// an example of using mouse events on an engine.input.mouse
// an example of using mouse events on a mouse
Events.on(_mouseConstraint, 'mousedown', function(event) {
var mousePosition = event.mouse.position;
console.log('mousedown at ' + mousePosition.x + ' ' + mousePosition.y);
Expand All @@ -1096,14 +1081,42 @@

_sceneEvents.push(

// an example of using mouse events on an engine.input.mouse
// an example of using mouse events on a mouse
Events.on(_mouseConstraint, 'mouseup', function(event) {
var mousePosition = event.mouse.position;
_engine.render.options.background = "white";
console.log('mouseup at ' + mousePosition.x + ' ' + mousePosition.y);
})

);

// scene code

var stack = Composites.stack(50, 100, 8, 4, 50, 50, function(x, y, column, row) {
return Bodies.circle(x, y, 15, { restitution: 1, render: { strokeStyle: '#777' } });
});

World.add(_world, stack);

var renderOptions = _engine.render.options;
renderOptions.wireframes = false;

var shakeScene = function(engine) {
var bodies = Composite.allBodies(engine.world);

for (var i = 0; i < bodies.length; i++) {
var body = bodies[i];

if (!body.isStatic && body.position.y >= 500) {
var forceMagnitude = 0.01 * body.mass;

Body.applyForce(body, { x: 0, y: 0 }, {
x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]),
y: -forceMagnitude + Common.random() * -forceMagnitude
});
}
}
};
};

Demo.sprites = function() {
Expand Down Expand Up @@ -1335,6 +1348,11 @@
Events.off(_mouseConstraint, _sceneEvents[i]);
}

if (_world.events) {
for (i = 0; i < _sceneEvents.length; i++)
Events.off(_world, _sceneEvents[i]);
}

_sceneEvents = [];

// reset id pool
Expand Down

0 comments on commit 296d47e

Please sign in to comment.