Skip to content

Commit 296d47e

Browse files
committed
added composite events example to Demo.events
1 parent 2841522 commit 296d47e

1 file changed

Lines changed: 42 additions & 24 deletions

File tree

demo/js/Demo.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -994,32 +994,17 @@
994994
var _world = _engine.world;
995995

996996
Demo.reset();
997-
998-
var stack = Composites.stack(50, 100, 8, 4, 50, 50, function(x, y, column, row) {
999-
return Bodies.circle(x, y, 15, { restitution: 1, render: { strokeStyle: '#777' } });
1000-
});
1001-
1002-
World.add(_world, stack);
1003-
1004-
var renderOptions = _engine.render.options;
1005-
renderOptions.wireframes = false;
1006997

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

1010-
for (var i = 0; i < bodies.length; i++) {
1011-
var body = bodies[i];
1000+
_sceneEvents.push(
10121001

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

1016-
Body.applyForce(body, { x: 0, y: 0 }, {
1017-
x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]),
1018-
y: -forceMagnitude + Common.random() * -forceMagnitude
1019-
});
1020-
}
1021-
}
1022-
};
1007+
);
10231008

10241009
_sceneEvents.push(
10251010

@@ -1084,7 +1069,7 @@
10841069

10851070
_sceneEvents.push(
10861071

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

10971082
_sceneEvents.push(
10981083

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

11061091
);
1092+
1093+
// scene code
1094+
1095+
var stack = Composites.stack(50, 100, 8, 4, 50, 50, function(x, y, column, row) {
1096+
return Bodies.circle(x, y, 15, { restitution: 1, render: { strokeStyle: '#777' } });
1097+
});
1098+
1099+
World.add(_world, stack);
1100+
1101+
var renderOptions = _engine.render.options;
1102+
renderOptions.wireframes = false;
1103+
1104+
var shakeScene = function(engine) {
1105+
var bodies = Composite.allBodies(engine.world);
1106+
1107+
for (var i = 0; i < bodies.length; i++) {
1108+
var body = bodies[i];
1109+
1110+
if (!body.isStatic && body.position.y >= 500) {
1111+
var forceMagnitude = 0.01 * body.mass;
1112+
1113+
Body.applyForce(body, { x: 0, y: 0 }, {
1114+
x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]),
1115+
y: -forceMagnitude + Common.random() * -forceMagnitude
1116+
});
1117+
}
1118+
}
1119+
};
11071120
};
11081121

11091122
Demo.sprites = function() {
@@ -1335,6 +1348,11 @@
13351348
Events.off(_mouseConstraint, _sceneEvents[i]);
13361349
}
13371350

1351+
if (_world.events) {
1352+
for (i = 0; i < _sceneEvents.length; i++)
1353+
Events.off(_world, _sceneEvents[i]);
1354+
}
1355+
13381356
_sceneEvents = [];
13391357

13401358
// reset id pool

0 commit comments

Comments
 (0)