diff --git a/src/body/Composite.js b/src/body/Composite.js index 06fc6ef..f91b4f2 100644 --- a/src/body/Composite.js +++ b/src/body/Composite.js @@ -166,7 +166,7 @@ var Composite = {}; * @return {composite} The original compositeA with the composite removed */ Composite.removeComposite = function(compositeA, compositeB, deep) { - var position = compositeA.composites.indexOf(compositeB); + var position = Common.indexOf(compositeA.composites, compositeB); if (position !== -1) { Composite.removeCompositeAt(compositeA, position); Composite.setModified(compositeA, true, true, false); @@ -219,7 +219,7 @@ var Composite = {}; * @return {composite} The original composite with the body removed */ Composite.removeBody = function(composite, body, deep) { - var position = composite.bodies.indexOf(body); + var position = Common.indexOf(composite.bodies, body); if (position !== -1) { Composite.removeBodyAt(composite, position); Composite.setModified(composite, true, true, false); @@ -272,7 +272,7 @@ var Composite = {}; * @return {composite} The original composite with the constraint removed */ Composite.removeConstraint = function(composite, constraint, deep) { - var position = composite.constraints.indexOf(constraint); + var position = Common.indexOf(composite.constraints, constraint); if (position !== -1) { Composite.removeConstraintAt(composite, position); } diff --git a/src/collision/Grid.js b/src/collision/Grid.js index 4d7ace1..7b5ad17 100644 --- a/src/collision/Grid.js +++ b/src/collision/Grid.js @@ -247,7 +247,7 @@ var Grid = {}; */ var _bucketRemoveBody = function(grid, bucket, body) { // remove from bucket - bucket.splice(bucket.indexOf(body), 1); + bucket.splice(Common.indexOf(bucket, body), 1); // update pair counts for (var i = 0; i < bucket.length; i++) { diff --git a/src/collision/Pairs.js b/src/collision/Pairs.js index 61999d6..d74c5ec 100644 --- a/src/collision/Pairs.js +++ b/src/collision/Pairs.js @@ -85,7 +85,7 @@ var Pairs = {}; // deactivate previously active pairs that are now inactive for (i = 0; i < pairsList.length; i++) { pair = pairsList[i]; - if (pair.isActive && activePairIds.indexOf(pair.id) === -1) { + if (pair.isActive && Common.indexOf(activePairIds, pair.id) === -1) { Pair.setActive(pair, false, timestamp); collisionEnd.push(pair); } diff --git a/src/core/Common.js b/src/core/Common.js index 8b7a29a..fbd4c27 100644 --- a/src/core/Common.js +++ b/src/core/Common.js @@ -281,6 +281,24 @@ var Common = {}; return Common._nextId++; }; + /** + * A cross browser compatible indexOf implementation + * @method indexOf + * @param {array} haystack + * @param {object} needle + */ + Common.indexOf = function(haystack, needle) { + if (haystack.indexOf) + return haystack.indexOf(needle); + + for (var i = 0; i < haystack.length; i++) { + if (haystack[i] === needle) + return i; + } + + return -1; + }; + var _seededRandom = function() { // https://gist.github.com/ngryman/3830489 Common._seed = (Common._seed * 9301 + 49297) % 233280; diff --git a/src/render/RenderPixi.js b/src/render/RenderPixi.js index 2dd1ca3..6bf6c1d 100644 --- a/src/render/RenderPixi.js +++ b/src/render/RenderPixi.js @@ -261,7 +261,7 @@ var RenderPixi = {}; } // add to scene graph if not already there - if (container.children.indexOf(primitive) === -1) + if (Common.indexOf(container.children, primitive) === -1) container.addChild(primitive); // render the constraint on every update, since they can change dynamically @@ -307,7 +307,7 @@ var RenderPixi = {}; sprite = render.sprites[spriteId] = _createBodySprite(render, body); // add to scene graph if not already there - if (spriteBatch.children.indexOf(sprite) === -1) + if (Common.indexOf(spriteBatch.children, sprite) === -1) spriteBatch.addChild(sprite); // update body sprite @@ -326,7 +326,7 @@ var RenderPixi = {}; } // add to scene graph if not already there - if (container.children.indexOf(primitive) === -1) + if (Common.indexOf(container.children, primitive) === -1) container.addChild(primitive); // update body primitive