Alright, should be easy, maybe I've been looking at it too long. I'm letting the user place a certain number of "flags" on an image (content_mc). I'm creating them with:
function newFlag(){
if(flagNum<flagTotal){
flagNum++; //this keeps track of the number of "flags" that are placed
var newFlag:flag = new flag();
contentPH_mc.addChild(newFlag);
}
}
Now, I need to be able to let the user remove a flag by clicking on it, and, when they're done, I need to test each of the flags to see if they're over another clip (target_mc) on the screen, but I'm not sure how to reference the flags...
Thanks...
Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.
it sounds like you're making minesweeper. Are you not? Well anyway
addChild can be difficult to work with. The main problem is that you want to have many interactive flags on the screen at once, that are all defined by a single variable. Also (Very Unfortunately ) you cannot addChild within another movieclip. The most annoying part about it is that somewhere along the lines of your code, you are going to have to reference every child individually if you want each one to be interactive. I took some of my time into coming up with a solution and I have an .fla for you. Creating it, actually helped me out with a similar issue of my own. Although, this should not be your solution, if you create 25 flags with the mouse in my solution and then delete them all and start creating again, you will have problems deleting them the second time. Good Luck.
Deejayo, your reply is very misleading and outright incorrect in places. You absolutely can add a child to any DisplayObjectContainer (all MovieClips are DisplayObjectContainers) even if it's already a child of another. And that has no bearing on the problem at hand.
I cannot open your fla to see how you solved it, but there is a very simple approach to organizing these instances: use an array.
Code:
var flags:Array = [];
function newFlag():void{
if (flags.length < flagTotal){
var newFlag:flag = new flag(); //classes should begin with an Uppercase letter. :(
flags.push(newFlag);
contentPH_mc.addChild(newFlag);
}
}
Then later you can iterate through all your flags easily:
Code:
for (var i:int = 0; i < flags.length; i++){
var f:flag = flags[i];
//do something with f
}
When removing, you'll want to use the event target or current target property to get a reference to the flag that was clicked on, then splice that flag out of the array:
Code:
function removeFlag(f:flag):void{
var idx:int = flags.indexOf(f);
if (idx != -1){
if (f.parent){
contentPH_mc.removeChild(f);
}
flags.splice(idx, 1);
}
}