删除子项不起作用只删除其中的一半

时间:2016-03-11 22:35:59

标签: javascript three.js

在这段代码中,如果删除其中一半,我就无法删除所有孩子?当我定义它时,我得到错误child没有尊严?每个节点都有子球体(nods)和边缘(线)这种方法只删除点头,为什么?有人可以帮我吗?

function onMouseClick( e ) {


    mouseVector.x = 2 * (e.clientX / containerWidth) - 1;
    mouseVector.y = 1 - 2 * ( e.clientY / containerHeight );


    var raycaster = projector.pickingRay( mouseVector.clone(), camera ),
    intersects = raycaster.intersectObjects( scene.children );


    for( var i = 0; i < intersects.length; i++ ) {
        //INTERSECTED = intersects[0].object;

        INTERSECTED = intersects[i].object;
        //obj = intersection.object;

        alert(INTERSECTED.id);

        /*1-*/      //this
        //scene.remove(INTERSECTED);

        /*2-*/      //or this
        for ( c = 0, cl = INTERSECTED.children.length; c < cl; c ++ ) {
            var child = INTERSECTED.children[ c ];
            alert(child.id);
            //child.parent.remove(obj);
            INTERSECTED.remove(child);
            //var lin = scene.children[child.id+1];

            //r lin = scene.getObjectById(child.id+1, true );
            // alert(child.id+1);
            // INTERSECTED.remove(scene.children[child.id+1]);
        }    

        //scene.remove(INTERSECTED);
        scene.add(INTERSECTED);

        animate();
    }
}

1 个答案:

答案 0 :(得分:0)

我认为问题在于你循环一个你正在改变长度的数组。这与three.js无关,但是您在javascript代码中犯了一个基本(初学者)错误 也许您应该重写代码并使用while循环:

while( INTERSECTED.children.length > 0 ){

    var child = INTERSECTED.children[ 0 ];
    INTERSECTED.remove(child);

}