1

So, in my script I have given the user the ability to make unlimited amount of a certain movieclip.

var square = new Square();
sqaure.x = mouseX;
sqaure.y = mouseY;
addChild(square);

However, I would like to have the script remove any extra children added to the same X and Y coordinates. I need to make sure it removes the extra child even if they click and move the cursor away and then click back to an already populated location later. Either in the .class file or in the main script itself.

Any ideas? Thanks

Koden
  • 353
  • 1
  • 13

2 Answers2

1

At the moment of click you can obtain a list of all the things under the mouse cursor with the getObjectsUnderPoint(...) method and remove any subset of them upon criteria of your liking.

// 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);
}
Organis
  • 7,243
  • 2
  • 12
  • 14
0

One thing that Organis said, the "addEventListener" is something you can take a look at using the search terms "as3 event listener api". the "api" searches will come up with adobe specific code and property examples.

You can try to put in small input text boxes and a button with an event listener to set x and y to the values of the input text boxes

Another thing, I've always done it best with arrays to hold every item that you are adding to the stage.

//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);

So this is a bunch of things that you can mess around with if you need ideas. I tend to search and search and then find a little bit of code to incorporate into my projects while not necessarily using every part of a specific code example.

Jyreeil
  • 75
  • 1
  • 11