Akhil
Akhil

Reputation: 443

How to remove a child movie clip created by other function

I have added a listener function for mouse event

bar.addEventListener(MouseEvent.MOUSE_OVER,mouse_over_bar);

And defined the function

public function mouse_over_bar(ev:MouseEvent):void{

                 var hover:MovieClip=new Hvr();
                 var tween:Tween;

                addChild(hover);
                hover.x=mouseX;
                hover.y=mouseY;

                TransitionManager.start(hover,{type:Fly,direction:Transition.IN,duration:1,easing:Strong.easeInOut});
                hover.Hd1.text= xmlFile.children()[2]. @ name;
                hover.descrpt.text= xmlFile.children()[2]. @ des;

                }

How i remove this child movieclip hover? I would like to remove the chil when i am roll out from the bar.

Upvotes: 1

Views: 844

Answers (3)

Roy
Roy

Reputation: 325

One way would be to keep all your "hover" MCs in an Array, and in rollout remove them all at once. This way you don't need to keep track of individual movieclips which can become buggy.

private var hovers:Array = new Array();

public function mouse_over_bar(ev:MouseEvent):void{
    var hover:MovieClip = new Hvr();    
    addChild(hover);
    hovers.push(hover);
}

public function mouse_out_bar(ev:MouseEvent):void{
    while (hovers.length){
        var mc:MovieClip = hovers.pop();
        if (contains(mc)){
            removeChild(mc);
        }
    }
}

Upvotes: 0

23tux
23tux

Reputation: 14736

Look at the removeChildAt() method of a MovieClip. If you have only one child in your bar and a roll out listener, it could be sth like that:

public function mouse_over_bar(ev:MouseEvent):void{
   var currentBar : MovieClip = ev.target as MovieClip;
   currentBar.removeChildAt(0);
}

EDIT:

Uh I'm sorry, I thought you where adding the hover to the bar as a child. If you add the hover to the same parent movieclip as the bar is added, you have to store a reference to the hover outside the function. And if you have many hovers, you could store it in an array.

private var _hoverArray : Array = new Array()

public function mouse_over_bar(ev:MouseEvent):void{

                 var hover:MovieClip=new Hvr();
                 _hoverArray.push(hover);
                 var tween:Tween;

                addChild(hover);
                hover.x=mouseX;
                hover.y=mouseY;

                TransitionManager.start(hover,{type:Fly,direction:Transition.IN,duration:1,easing:Strong.easeInOut});
                hover.Hd1.text= xmlFile.children()[2]. @ name;
                hover.descrpt.text= xmlFile.children()[2]. @ des;

                }

Then you have to find a way, to indicate the hover object you want to remove. I would suggest you to define a new class called "hover", extends MovieClip, give it an ID, and also give your bars an ID. So if you roll out, you can search the _hoverArray for the ID and remove it.

public function mouse_over_bar(ev:MouseEvent):void{
   var currentBar : BarClass = ev.target as BarClass;
   var barID : int = currentBar.id;
   var currentHover : Hover;
   for each(var h : Hover in _hoverArry)
   {
          if(h.id == barID)
          {
                 currentHover = h;
                 break;
          }
   }

   if(currentHover)
          removeChild(currentHover);
}

Upvotes: 1

Taurayi
Taurayi

Reputation: 3207

I'm not quite sure how you structured you application but this should work.

bar.addEventListener(MouseEvent.MOUSE_OVER, onBarMouseOver); 
bar.addEventListener(MouseEvent.MOUSE_OUT, onBarMouseOut); 

public function onBarMouseOver(e:MouseEvent):void
{                  
    var hover:MovieClip = new Hvr();    
    hover.name = "hover";
    var tween:Tween;  
    addChild(hover);                
    hover.x = mouseX;     
    hover.y = mouseY;               
    TransitionManager.start(hover,{type:Fly,direction:Transition.IN,duration:1,easing:Strong.easeInOut});    
    hover.Hd1.text = xmlFile.children()[2]. @ name;                 
    hover.descrpt.text = xmlFile.children()[2]. @ des;   

}// end function

public function onBarMouseOut(e:MouseEvent):void
{   
    if(this.contains(getChildByName("hover"))
    removeChild(this.removeChild(this.getChildByName("hover")));

}// end function

Upvotes: 0

Related Questions