mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-27 09:50:52 -05:00
changed demo to properly track and clear events between scenes
This commit is contained in:
parent
5b013d01e7
commit
f530ed4f36
1 changed files with 105 additions and 78 deletions
|
@ -26,6 +26,7 @@
|
|||
_inspector,
|
||||
_sceneName,
|
||||
_mouseConstraint,
|
||||
_sceneEvents = [],
|
||||
_isMobile = /(ipad|iphone|ipod|android)/gi.test(navigator.userAgent);
|
||||
|
||||
// initialise the demo
|
||||
|
@ -631,6 +632,8 @@
|
|||
}
|
||||
};
|
||||
|
||||
_sceneEvents.push(
|
||||
|
||||
// an example of using beforeUpdate event on an engine
|
||||
Events.on(_engine, 'beforeUpdate', function(event) {
|
||||
var engine = event.source;
|
||||
|
@ -638,7 +641,11 @@
|
|||
// apply random forces every 5 secs
|
||||
if (event.timestamp % 5000 < 50)
|
||||
shakeScene(engine);
|
||||
});
|
||||
})
|
||||
|
||||
);
|
||||
|
||||
_sceneEvents.push(
|
||||
|
||||
// an example of using collisionStart event on an engine
|
||||
Events.on(_engine, 'collisionStart', function(event) {
|
||||
|
@ -650,7 +657,11 @@
|
|||
pair.bodyA.render.fillStyle = '#bbbbbb';
|
||||
pair.bodyB.render.fillStyle = '#bbbbbb';
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
);
|
||||
|
||||
_sceneEvents.push(
|
||||
|
||||
// an example of using collisionActive event on an engine
|
||||
Events.on(_engine, 'collisionActive', function(event) {
|
||||
|
@ -662,7 +673,11 @@
|
|||
pair.bodyA.render.fillStyle = '#aaaaaa';
|
||||
pair.bodyB.render.fillStyle = '#aaaaaa';
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
);
|
||||
|
||||
_sceneEvents.push(
|
||||
|
||||
// an example of using collisionEnd event on an engine
|
||||
Events.on(_engine, 'collisionEnd', function(event) {
|
||||
|
@ -674,7 +689,11 @@
|
|||
pair.bodyA.render.fillStyle = '#999999';
|
||||
pair.bodyB.render.fillStyle = '#999999';
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
);
|
||||
|
||||
_sceneEvents.push(
|
||||
|
||||
// an example of using mouse events on an engine.input.mouse
|
||||
Events.on(_engine, 'mousedown', function(event) {
|
||||
|
@ -682,14 +701,20 @@
|
|||
console.log('mousedown at ' + mousePosition.x + ' ' + mousePosition.y);
|
||||
_engine.render.options.background = 'cornsilk';
|
||||
shakeScene(_engine);
|
||||
});
|
||||
})
|
||||
|
||||
);
|
||||
|
||||
_sceneEvents.push(
|
||||
|
||||
// an example of using mouse events on an engine.input.mouse
|
||||
Events.on(_engine, 'mouseup', function(event) {
|
||||
var mousePosition = event.mouse.position;
|
||||
_engine.render.options.background = "white";
|
||||
console.log('mouseup at ' + mousePosition.x + ' ' + mousePosition.y);
|
||||
});
|
||||
})
|
||||
|
||||
);
|
||||
};
|
||||
|
||||
Demo.sprites = function() {
|
||||
|
@ -770,6 +795,7 @@
|
|||
|
||||
World.add(_world, stack);
|
||||
|
||||
_sceneEvents.push(
|
||||
Events.on(_engine, 'afterRender', function() {
|
||||
var mouse = _engine.input.mouse,
|
||||
context = _engine.render.context,
|
||||
|
@ -797,7 +823,8 @@
|
|||
|
||||
context.fillStyle = 'rgba(255,165,0,0.7)';
|
||||
context.fill();
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
var renderOptions = _engine.render.options;
|
||||
};
|
||||
|
@ -905,10 +932,10 @@
|
|||
if (renderController.clear)
|
||||
renderController.clear(_engine.render);
|
||||
|
||||
/*if (Events) {
|
||||
// clear all events
|
||||
Events.off(_engine);
|
||||
}*/
|
||||
// clear all scene events
|
||||
for (var i = 0; i < _sceneEvents.length; i++)
|
||||
Events.off(_engine, _sceneEvents[i]);
|
||||
_sceneEvents = [];
|
||||
|
||||
// reset id pool
|
||||
Common._nextId = 0;
|
||||
|
|
Loading…
Reference in a new issue