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

added body sleepStart and sleepEnd events, closes #91

This commit is contained in:
liabru 2015-05-24 15:56:54 +01:00
parent 796faeb069
commit bc26469df2
3 changed files with 43 additions and 0 deletions

View file

@ -862,6 +862,13 @@
World.add(_world, stack);
for (var i = 0; i < stack.bodies.length; i++) {
Events.on(stack.bodies[i], 'sleepStart sleepEnd', function(event) {
var body = this;
console.log('body id', body.id, 'sleeping:', body.isSleeping);
});
}
_engine.enableSleeping = true;
};

View file

@ -604,6 +604,32 @@ var Body = {};
return properties;
};
/*
*
* Events Documentation
*
*/
/**
* Fired when a body starts sleeping (where `this` is the body).
*
* @event sleepStart
* @this {body} The body that has started sleeping
* @param {} event An event object
* @param {} event.source The source object of the event
* @param {} event.name The name of the event
*/
/**
* Fired when a body ends sleeping (where `this` is the body).
*
* @event sleepEnd
* @this {body} The body that has ended sleeping
* @param {} event An event object
* @param {} event.source The source object of the event
* @param {} event.name The name of the event
*/
/*
*
* Properties Documentation

View file

@ -92,6 +92,8 @@ var Sleeping = {};
* @param {boolean} isSleeping
*/
Sleeping.set = function(body, isSleeping) {
var wasSleeping = body.isSleeping;
if (isSleeping) {
body.isSleeping = true;
body.sleepCounter = body.sleepThreshold;
@ -106,9 +108,17 @@ var Sleeping = {};
body.speed = 0;
body.angularSpeed = 0;
body.motion = 0;
if (!wasSleeping) {
Events.trigger(body, 'sleepStart');
}
} else {
body.isSleeping = false;
body.sleepCounter = 0;
if (wasSleeping) {
Events.trigger(body, 'sleepEnd');
}
}
};