mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-27 09:50:52 -05:00
added Composite.get
This commit is contained in:
parent
57c6e29105
commit
e1b6f9ba5a
1 changed files with 34 additions and 0 deletions
|
@ -358,4 +358,38 @@ var Composite = {};
|
|||
return composites;
|
||||
};
|
||||
|
||||
/**
|
||||
* Searches the composite recursively for an object matching the type and id supplied, null if not found
|
||||
* @method get
|
||||
* @param {composite} composite
|
||||
* @param {string} type
|
||||
* @param {number} id
|
||||
* @return {object} The requested object, if found
|
||||
*/
|
||||
Composite.get = function(composite, type, id) {
|
||||
var objects,
|
||||
object;
|
||||
|
||||
switch (type) {
|
||||
case 'body':
|
||||
objects = Composite.allBodies(composite);
|
||||
break;
|
||||
case 'constraint':
|
||||
objects = Composite.allConstraints(composite);
|
||||
break;
|
||||
case 'composite':
|
||||
objects = Composite.allComposites(composite);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!objects)
|
||||
return null;
|
||||
|
||||
object = objects.filter(function(object) {
|
||||
return object.id.toString() === id.toString();
|
||||
});
|
||||
|
||||
return object.length === 0 ? null : object[0];
|
||||
};
|
||||
|
||||
})();
|
Loading…
Reference in a new issue