Actionscript 3 如果在同一位置或发生碰撞,如何删除同名子项?AS3

Actionscript 3 如果在同一位置或发生碰撞,如何删除同名子项?AS3,actionscript-3,removechild,addchild,Actionscript 3,Removechild,Addchild,因此,在我的脚本中,我给了用户无限量制作某个电影片段的能力 var square = new Square(); sqaure.x = mouseX; sqaure.y = mouseY; addChild(square); 但是,我想让脚本删除添加到相同X和Y坐标的所有额外子项。我需要确保它删除额外的子项,即使他们单击并移动光标,然后稍后单击返回到已填充的位置。在.class文件或主脚本本身中 有什么想法吗?谢谢单击时,您可以使用该方法获得鼠标光标下所有内容的列表,并根据您的喜好删除其中的任

因此,在我的脚本中,我给了用户无限量制作某个电影片段的能力

var square = new Square();
sqaure.x = mouseX;
sqaure.y = mouseY;
addChild(square);
但是,我想让脚本删除添加到相同X和Y坐标的所有额外子项。我需要确保它删除额外的子项,即使他们单击并移动光标,然后稍后单击返回到已填充的位置。在.class文件或主脚本本身中


有什么想法吗?谢谢

单击时,您可以使用该方法获得鼠标光标下所有内容的列表,并根据您的喜好删除其中的任何子集

// Stage, because if user clicks the current container
// into the empty area, the click won't register.
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);

function onDown(e:MouseEvent):void
{
    var aPoint:Point = new Point;

    // Set it to the mouse coordinates.
    aPoint.x = mouseX;
    aPoint.y = mouseY;

    // Convert it to Stage coordinates.
    aPoint = localToGlobal(aPoint);

    // The returned list will contain only descendants
    // of the current DisplayObjectContainer.
    var aList:Array = getObjectsUnderPoint(aPoint);

    // Iterate through the results.
    for each (var aChild:DiaplayObject in aList)
    {
        // Now, filter the results to match certain criteria.

        // Don't bother with the grandchildren.
        if (aChild.parent != this) continue;

        // Ignore things if they are not of the right class.
        if (!(aChild is Square)) continue;

        // ...etc.

        // Remove those ones that have passed all the checks.
        removeChild(aChild);
    }

    // Add the new one here.

    var aSq:Square = new Square;

    aSq.x = mouseX;
    aSq.y = mouseY;

    addChild(aSq);
}

Organi说的一件事,“addEventListener”是您可以使用搜索词“as3事件侦听器api”来查看的内容。“api”搜索将提供adobe特定的代码和属性示例

您可以尝试放入小的输入文本框和带有事件侦听器的按钮,以将x和y设置为输入文本框的值

另一件事,我一直都在使用数组来保存您添加到舞台上的每个项目

//global variables
    var nameSprite:Sprite;
    var name2Array:Array = new Array();
    var id:Number = 0;

//initial function
    nameSprite = new Sprite();
    addChild(nameSprite);
    name2Array = new Array();//seems redundant but has been what I've had to do to make it work

//other function to add items to the array
    var sqaure:objectName = new objectName();
    sqaure.x = mouseX;
    sqaure.y = mouseY;
    square.id = id;//I like to add an id to be able to better sort things
    nameSprite.addChild(sqaure);
    name2Array.push(sqaure);
    id++;

//function to remove items
    IndexPH = j;//j would be the index in the for loop to identify the entry to be removed
    nameSprite.removeChild(name2Array[IndexPH]);//removes from stage
    name2Array.splice(IndexPH,1);//removes from array

//function to sort the array after an item has been removed
    name2Array.sortOn(["id"], Array.NUMERIC);
如果你需要创意,这是一堆你可以胡乱处理的事情。我倾向于搜索一个又一个,然后找到一点代码来合并到我的项目中,而不一定使用特定代码示例的每个部分