Browse Source

✨ Add some missing functions to GameObject

- Add function to get a child by its id,
- Add functions to remove a child by its name and index.
Pedro Schneider 3 years ago
parent
commit
b62ee9f8c2
1 changed files with 28 additions and 0 deletions
  1. 28 0
      pandora/game_objects/GameObject.js

+ 28 - 0
pandora/game_objects/GameObject.js

@@ -52,6 +52,16 @@ class GameObject
         return null;
     }
 
+    getChildById(id)
+    {
+        for (let i = 0; i < this.children.length; i++)
+        {
+            if (this.children[i].id == id)
+                return this.children[i];
+        }
+        return null;
+    }
+
     getChildByName(name)
     {
         for (let i = 0; i < this.children.length; i++)
@@ -106,6 +116,12 @@ class GameObject
         if (this.isOnTree) child.setup();
     }
 
+    removeChildByIndex(idx)
+    {
+        if (idx >= 0 && idx < this.children.length)
+            this.children.splice(idx, 1);
+    }
+
     removeChildById(id)
     {
         for (let i = 0; i < this.children.length; i++)
@@ -118,6 +134,18 @@ class GameObject
         }
     }
 
+    removeChildByName(name)
+    {
+        for (let i = 0; i < this.children.length; i++)
+        {
+            if (this.children[i].name == name)
+            {
+                this.children.splice(i, 1);
+                return;
+            }
+        }
+    }
+
     free()
     {
         if (this.parented)