Kineticjs 如何从组中完全删除矩形?

Kineticjs 如何从组中完全删除矩形?,kineticjs,Kineticjs,我想实现一个函数,它将一个组和一个矩形作为参数,并检查该矩形是否属于该组。如果它属于组中,我想从组和层中完全删除该矩形。我尝试了group.remove()和group.getChildren()[I].remove,但我认为它们并没有完全删除矩形。经过一番努力,我终于做到了以下我觉得很愚蠢的事情: /** * Checks if a rectangle belongs in any of the existing groups */ function removesFromGroup(gr

我想实现一个函数,它将一个组和一个矩形作为参数,并检查该矩形是否属于该组。如果它属于组中,我想从组和层中完全删除该矩形。我尝试了group.remove()和group.getChildren()[I].remove,但我认为它们并没有完全删除矩形。经过一番努力,我终于做到了以下我觉得很愚蠢的事情:

/**
 * Checks if a rectangle belongs in any of the existing groups
 */
function removesFromGroup(group, tempRect) {

    /**
     * I created a bin group and I send the rectangle to this group by using group.moveTo(bin)
     */
    var bin = new Kinetic.Group({
        name: 'bin',
        x: -480,
        y: -50
    });

    for (var j = 0; j < group.getChildren().length; j++) {
        //  var groupCube_x = (groups[i].getChildren()[j]).getAbsolutePosition().x;

        if (group.getChildren()[j] == tempRect) {
            tempRect.moveTo(bin);

            // alert("Move to bin");
            //tempRect.remove();
            //tempRect.getParent().remove(bin);

            //bin.destroy(); 


            layer.draw();
        }
    }
    return false;
}
/**
*检查矩形是否属于任何现有组
*/
函数removesFromGroup(组,tempRect){
/**
*我创建了一个bin组,并使用group.moveTo(bin)将矩形发送到此组
*/
var bin=新的动力学组({
名称:“bin”,
x:-480,
y:-50
});
对于(var j=0;j
然而,即使这对一个案例有效,我仍然面临着一个问题,即我不知道如何从层或阶段中完全移除(删除)矩形或组

如果不清楚的话,我可以提供更多关于我到底想做什么的信息。

每个动力学节点(形状、层、组等)都有一个名为
destroy()
的方法,该方法可以移除和销毁自身,另请参见此处:

因此,在本例中,您可以调用
tempRect.destroy()
,矩形将从任何地方移除并自行销毁。当然,您需要在它的一个父对象上执行
draw()
,但您已经在这样做了。您也可以删除您的bin组,这应该不再需要:-)


Javascript还提供了一个很好的方法()来检查元素是否是数组的一部分,因此可以完全跳过循环。然后,在确认节点是组的一部分后,可以使用
.destroy()
方法来销毁它

function removesFromGroup(group, tempRect) {
    if (group.getChildren().indexOf(tempRect) !== -1)
        tempRect.destroy();
        layer.draw();
    });

    return false;
}

我知道会有类似的情况出现,我应该进一步检查数组和对象上的Javascript api。很好的改进。
function removesFromGroup(group, tempRect) {
    if (group.getChildren().indexOf(tempRect) !== -1)
        tempRect.destroy();
        layer.draw();
    });

    return false;
}