Skip to content

Commit

Permalink
Merge pull request #20478 from Mugen87/dev44
Browse files Browse the repository at this point in the history
Object3D: Add removeAll().
  • Loading branch information
mrdoob authored Oct 8, 2020
2 parents a95af8f + c5faa7f commit 68976a1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/api/en/core/Object3D.html
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ <h3>[method:this remove]( [param:Object3D object], ... )</h3>
Removes *object* as child of this object. An arbitrary number of objects may be removed.
</p>

<h3>[method:this removeAll]()</h3>
<p>
Removes all child objects.
</p>

<h3>[method:this rotateOnAxis]( [param:Vector3 axis], [param:Float angle] )</h3>
<p>
axis -- A normalized vector in object space. <br />
Expand Down
5 changes: 5 additions & 0 deletions src/core/Object3D.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ export class Object3D extends EventDispatcher {
*/
remove( ...object: Object3D[] ): this;

/**
* Removes all child objects.
*/
removeAll(): this;

/**
* Adds object as a child of this, while maintaining the object's world transform.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/core/Object3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,25 @@ Object3D.prototype = Object.assign( Object.create( EventDispatcher.prototype ),

},

removeAll: function () {

for ( let i = 0; i < this.children.length; i ++ ) {

const object = this.children[ i ];

object.parent = null;

object.dispatchEvent( _removedEvent );

}

this.children.length = 0;

return this;


},

attach: function ( object ) {

// adds object as a child of this, while maintaining the object's world transform
Expand Down
9 changes: 8 additions & 1 deletion test/unit/src/core/Object3D.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export default QUnit.module( 'Core', () => {

} );

QUnit.test( "add/remove", ( assert ) => {
QUnit.test( "add/remove/removeAll", ( assert ) => {

var a = new Object3D();
var child1 = new Object3D();
Expand Down Expand Up @@ -328,6 +328,13 @@ export default QUnit.module( 'Core', () => {
assert.strictEqual( a.children[ 0 ], child2, "The second one is now the parent's child again" );
assert.strictEqual( child1.children.length, 0, "The first one no longer has any children" );

a.add( child1 );
assert.strictEqual( a.children.length, 2, "The first child was added to the parent" );
a.removeAll();
assert.strictEqual( a.children.length, 0, "All childrens were removed" );
assert.strictEqual( child1.parent, null, "First child has no parent" );
assert.strictEqual( child2.parent, null, "Second child has no parent" );

} );

QUnit.test( "getObjectById/getObjectByName/getObjectByProperty", ( assert ) => {
Expand Down

0 comments on commit 68976a1

Please sign in to comment.