Skip to content

Commit

Permalink
added body sleepStart and sleepEnd events, closes #91
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed May 24, 2015
1 parent 796faeb commit bc26469
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions demo/js/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
26 changes: 26 additions & 0 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/core/Sleeping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
}
}
};

Expand Down

0 comments on commit bc26469

Please sign in to comment.