|
994 | 994 | var _world = _engine.world; |
995 | 995 |
|
996 | 996 | 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; |
1006 | 997 |
|
1007 | | - var shakeScene = function(engine) { |
1008 | | - var bodies = Composite.allBodies(engine.world); |
| 998 | + // bind events (_sceneEvents is only used for this demo) |
1009 | 999 |
|
1010 | | - for (var i = 0; i < bodies.length; i++) { |
1011 | | - var body = bodies[i]; |
| 1000 | + _sceneEvents.push( |
1012 | 1001 |
|
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 | + }) |
1015 | 1006 |
|
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 | + ); |
1023 | 1008 |
|
1024 | 1009 | _sceneEvents.push( |
1025 | 1010 |
|
|
1084 | 1069 |
|
1085 | 1070 | _sceneEvents.push( |
1086 | 1071 |
|
1087 | | - // an example of using mouse events on an engine.input.mouse |
| 1072 | + // an example of using mouse events on a mouse |
1088 | 1073 | Events.on(_mouseConstraint, 'mousedown', function(event) { |
1089 | 1074 | var mousePosition = event.mouse.position; |
1090 | 1075 | console.log('mousedown at ' + mousePosition.x + ' ' + mousePosition.y); |
|
1096 | 1081 |
|
1097 | 1082 | _sceneEvents.push( |
1098 | 1083 |
|
1099 | | - // an example of using mouse events on an engine.input.mouse |
| 1084 | + // an example of using mouse events on a mouse |
1100 | 1085 | Events.on(_mouseConstraint, 'mouseup', function(event) { |
1101 | 1086 | var mousePosition = event.mouse.position; |
1102 | 1087 | _engine.render.options.background = "white"; |
1103 | 1088 | console.log('mouseup at ' + mousePosition.x + ' ' + mousePosition.y); |
1104 | 1089 | }) |
1105 | 1090 |
|
1106 | 1091 | ); |
| 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 | + }; |
1107 | 1120 | }; |
1108 | 1121 |
|
1109 | 1122 | Demo.sprites = function() { |
|
1335 | 1348 | Events.off(_mouseConstraint, _sceneEvents[i]); |
1336 | 1349 | } |
1337 | 1350 |
|
| 1351 | + if (_world.events) { |
| 1352 | + for (i = 0; i < _sceneEvents.length; i++) |
| 1353 | + Events.off(_world, _sceneEvents[i]); |
| 1354 | + } |
| 1355 | + |
1338 | 1356 | _sceneEvents = []; |
1339 | 1357 |
|
1340 | 1358 | // reset id pool |
|
0 commit comments