views:

30

answers:

2

I'm not even sure if the title reflects what I want to do. Here's backstory. I have a movieclip with 5 'holder' movieclips in them. They are my containers for dynamically added movieclips.

The parent containers has an instance name of pyramid, the holder instance names are labeled after the 6 food groups, i.e., 'grainholder', 'fruitholder', 'vegetableholder', etc etc...

The holders are within the pyramid container, instance names as above, and the pyramid container is added to the display list. I have a addFoodToPyramid() function which adds 'foodMC' mc's to their respective holders. So an [object Apple] will be dynamically added by way of pyramid.fruitholder.addChild(Apple). The function also adds a listener. Heres the call and the function:

The call (I will use grains as example) is this:

addFoodToPyramid(grainArray, grainHolder, grainCounter);

And the function is:

function addFoodToPyramid(thisArray:Array, thisHolder:MovieClip, thisCounter:Number):void {

   thisCounter = 0;

   for (var f:Number=0; f<thisArray.length; f++)
   {

    trace(thisArray.length);
    foodMC = (thisArray[f] as MovieClip);
    trace(foodMC);
    thisHolder.addChild(foodMC);
    foodMC.addEventListener(MouseEvent.MOUSE_DOWN, startFoodDrag, false, 0, true);
    thisMC.scaleX = thisMC.scaleY = .7;
    thisMC.x = 60 * thisCounter;
    thisCounter++;

   }

  }

Then I have a startDrag function:

function startFoodDrag(e:MouseEvent):void

{ foodDrag = e.target as MovieClip; foodDrag.startDrag(); foodPoint = new Point(foodDrag.x,foodDrag.y); stage.addEventListener(MouseEvent.MOUSE_UP, stopFoodDrag, false, 0, true); }

Then the drop function AND THE PROBLEM LIES HERE:

foodDrag = foodDrop;
stage.addEventListener(MouseEvent.MOUSE_UP, stopFoodDrag);
if(foodDrag.dropTarget.parent==myPlate){

//HERES THE PROBLEM removeChild(foodDrag); //obviously returns error because the foodDrag reference is wrong

}

Ok, so obviously the reference is not a direct removeChild() but a specific path. Like:

pyramid.*THE-HOLDER-OF-foodDRAG*.removeChild(foodDrag). 

But I just can't get my head around how to do it. I've tried:

var mcParent:MovieClip=(foodDroppedMC.parent);
pyramid.mcParent.removeChild(foodDroppedMC);

// returns error 1118 Implicit Coercion

I've tried:

var mcParent:String=(foodDroppedMC.parent.name);
pyramid.getChildByName(mcParent).removeChild(foodDroppedMC);

// return 1061 Call to possibly undefined method removeChild with ref to static type etc etc

The fact that it's such a banal problem is what makes it so frustrating...

Any help would be appreciated. Thanks

A: 

I'm having a bit of trouble following the details of your question (I think maybe some of the "drop function" got lost in your post somehow).

But if the question is: how do I remove a movie clip from its parent, without knowing an exact path to the parent? then the answer is simply:

foodDrag.parent.removeChild(foodDrag);

assuming that foodDrag is your movie clip that you want to remove.

If I'm missing the point, please reply and help me understand what you're asking.

Lee
A: 

Tried this AND IT WORKS!!!:

var mcParent:String=(foodDroppedMC.parent.name);
pyramid[mcParent].removeChild(foodDroppedMC);

If anyone can give some insight as to how this works I'd love to know. I assume the mcParent with type of "String" is make the (foodDroppedMC.parent.name) a string and the the [] brackets in the mc path are somehow casting it as a MC so there is no Implicit Coercion. That's my guess... lol.

Anyway, happy days. Feel free to expand on this solution. I'm not really sure Google University (where I got my Flash as3 degree =P) teaches much about the []'s being used other than with Arrays.

Snowden
the `[]` operator applied to an object, treats that object as an associative hash. It allows you to access properties of that object using a string containing the name of the property. [Check adobe's documentation](http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7eea.html) for a thorough explanation of associative hashes. (incidentally, the link above is part of a much larger document that you may find very helpful).
Lee
I should also point out that, if you just want to remove `foodDroppedMC`, all you have to do is `foodDropMC.parent.removeChild(foodDroppedMC)`. That's all. You don't need to resolve the parent through the `pyramid` object.
Lee