diff --git a/demo/js/Demo.js b/demo/js/Demo.js index ab1e71a4..2d23dcd3 100644 --- a/demo/js/Demo.js +++ b/demo/js/Demo.js @@ -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; }; diff --git a/src/body/Body.js b/src/body/Body.js index 82888c89..c6fd9f83 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -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 diff --git a/src/core/Sleeping.js b/src/core/Sleeping.js index 5ceaf958..799ec115 100644 --- a/src/core/Sleeping.js +++ b/src/core/Sleeping.js @@ -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'); + } } };