浏览代码

Merge pull request #4292 from RaananW/physics-onCollide-unregister

Physics on collide unregister
David Catuhe 7 年之前
父节点
当前提交
4206f4b921
共有 2 个文件被更改,包括 16 次插入2 次删除
  1. 1 0
      dist/preview release/what's new.md
  2. 15 2
      src/Physics/babylon.physicsImpostor.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -24,6 +24,7 @@
 ### Core Engine
 
 - Fix ```shadowEnabled``` property on lights. Shadows are not visble anymore when disabled ([sebavan](http://www.github.com/sebavan))
+- Physics `unregisterOnPhysicsCollide` didn't remove callback correctly [#4291](https://github.com/BabylonJS/Babylon.js/issues/4291) ([RaananW](https://github.com/RaananW))
 
 ### Viewer
 

+ 15 - 2
src/Physics/babylon.physicsImpostor.ts

@@ -358,9 +358,22 @@ module BABYLON {
 
         public unregisterOnPhysicsCollide(collideAgainst: PhysicsImpostor | Array<PhysicsImpostor>, func: (collider: PhysicsImpostor, collidedAgainst: PhysicsImpostor | Array<PhysicsImpostor>) => void): void {
             var collidedAgainstList: Array<PhysicsImpostor> = collideAgainst instanceof Array ? <Array<PhysicsImpostor>>collideAgainst : [<PhysicsImpostor>collideAgainst]
-            var index = this._onPhysicsCollideCallbacks.indexOf({ callback: func, otherImpostors: collidedAgainstList });
+            var index = -1;
+            let found = this._onPhysicsCollideCallbacks.some((cbDef, idx) => {
+                if (cbDef.callback === func && cbDef.otherImpostors.length === collidedAgainstList.length) {
+                    // chcek the arrays match
+                    let sameList = cbDef.otherImpostors.every((impostor) => {
+                        return collidedAgainstList.indexOf(impostor) > -1;
+                    });
+                    if (sameList) {
+                        index = idx;
+                    }
+                    return sameList;
+                }
+                return false;
+            });
 
-            if (index > -1) {
+            if (found) {
                 this._onPhysicsCollideCallbacks.splice(index, 1);
             } else {
                 Tools.Warn("Function to remove was not found");