0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-30 10:20:52 -05:00

changed demo to properly track and clear events between scenes

This commit is contained in:
liabru 2014-04-30 10:33:37 +01:00
parent 5b013d01e7
commit f530ed4f36

View file

@ -26,6 +26,7 @@
_inspector, _inspector,
_sceneName, _sceneName,
_mouseConstraint, _mouseConstraint,
_sceneEvents = [],
_isMobile = /(ipad|iphone|ipod|android)/gi.test(navigator.userAgent); _isMobile = /(ipad|iphone|ipod|android)/gi.test(navigator.userAgent);
// initialise the demo // initialise the demo
@ -631,6 +632,8 @@
} }
}; };
_sceneEvents.push(
// an example of using beforeUpdate event on an engine // an example of using beforeUpdate event on an engine
Events.on(_engine, 'beforeUpdate', function(event) { Events.on(_engine, 'beforeUpdate', function(event) {
var engine = event.source; var engine = event.source;
@ -638,7 +641,11 @@
// apply random forces every 5 secs // apply random forces every 5 secs
if (event.timestamp % 5000 < 50) if (event.timestamp % 5000 < 50)
shakeScene(engine); shakeScene(engine);
}); })
);
_sceneEvents.push(
// an example of using collisionStart event on an engine // an example of using collisionStart event on an engine
Events.on(_engine, 'collisionStart', function(event) { Events.on(_engine, 'collisionStart', function(event) {
@ -650,7 +657,11 @@
pair.bodyA.render.fillStyle = '#bbbbbb'; pair.bodyA.render.fillStyle = '#bbbbbb';
pair.bodyB.render.fillStyle = '#bbbbbb'; pair.bodyB.render.fillStyle = '#bbbbbb';
} }
}); })
);
_sceneEvents.push(
// an example of using collisionActive event on an engine // an example of using collisionActive event on an engine
Events.on(_engine, 'collisionActive', function(event) { Events.on(_engine, 'collisionActive', function(event) {
@ -662,7 +673,11 @@
pair.bodyA.render.fillStyle = '#aaaaaa'; pair.bodyA.render.fillStyle = '#aaaaaa';
pair.bodyB.render.fillStyle = '#aaaaaa'; pair.bodyB.render.fillStyle = '#aaaaaa';
} }
}); })
);
_sceneEvents.push(
// an example of using collisionEnd event on an engine // an example of using collisionEnd event on an engine
Events.on(_engine, 'collisionEnd', function(event) { Events.on(_engine, 'collisionEnd', function(event) {
@ -674,7 +689,11 @@
pair.bodyA.render.fillStyle = '#999999'; pair.bodyA.render.fillStyle = '#999999';
pair.bodyB.render.fillStyle = '#999999'; pair.bodyB.render.fillStyle = '#999999';
} }
}); })
);
_sceneEvents.push(
// an example of using mouse events on an engine.input.mouse // an example of using mouse events on an engine.input.mouse
Events.on(_engine, 'mousedown', function(event) { Events.on(_engine, 'mousedown', function(event) {
@ -682,14 +701,20 @@
console.log('mousedown at ' + mousePosition.x + ' ' + mousePosition.y); console.log('mousedown at ' + mousePosition.x + ' ' + mousePosition.y);
_engine.render.options.background = 'cornsilk'; _engine.render.options.background = 'cornsilk';
shakeScene(_engine); shakeScene(_engine);
}); })
);
_sceneEvents.push(
// an example of using mouse events on an engine.input.mouse // an example of using mouse events on an engine.input.mouse
Events.on(_engine, 'mouseup', function(event) { Events.on(_engine, 'mouseup', function(event) {
var mousePosition = event.mouse.position; var mousePosition = event.mouse.position;
_engine.render.options.background = "white"; _engine.render.options.background = "white";
console.log('mouseup at ' + mousePosition.x + ' ' + mousePosition.y); console.log('mouseup at ' + mousePosition.x + ' ' + mousePosition.y);
}); })
);
}; };
Demo.sprites = function() { Demo.sprites = function() {
@ -770,6 +795,7 @@
World.add(_world, stack); World.add(_world, stack);
_sceneEvents.push(
Events.on(_engine, 'afterRender', function() { Events.on(_engine, 'afterRender', function() {
var mouse = _engine.input.mouse, var mouse = _engine.input.mouse,
context = _engine.render.context, context = _engine.render.context,
@ -797,7 +823,8 @@
context.fillStyle = 'rgba(255,165,0,0.7)'; context.fillStyle = 'rgba(255,165,0,0.7)';
context.fill(); context.fill();
}); })
);
var renderOptions = _engine.render.options; var renderOptions = _engine.render.options;
}; };
@ -905,10 +932,10 @@
if (renderController.clear) if (renderController.clear)
renderController.clear(_engine.render); renderController.clear(_engine.render);
/*if (Events) { // clear all scene events
// clear all events for (var i = 0; i < _sceneEvents.length; i++)
Events.off(_engine); Events.off(_engine, _sceneEvents[i]);
}*/ _sceneEvents = [];
// reset id pool // reset id pool
Common._nextId = 0; Common._nextId = 0;