Skip navigation
Currently Being Moderated

closing a movie clip with EventListener with a mouse click

May 8, 2012 1:59 PM

I have a button that loads a movie clip and I want to be able to close that same movieclip by clicking anywhere.

 

This is my code so far:

 

btn_2.addEventListener(MouseEvent.CLICK, goBio);

function goBio (e:MouseEvent):void {

var newBio:BioToc = new BioToc();

this.addChild(newBio);

newBio.x = 25;

newBio.y = 45;

}

 
Replies
  • Currently Being Moderated
    May 8, 2012 2:03 PM   in reply to goodsnow82

    Since that object is declared inside a function it will be harder to target, so move the declaration outside the function...

     

    var newBio:BioToc;

     

    and just use

     

    newBio =  new BioToc();

     

    inside the function.

     

    Then you will be able to directly target the newBio instance.

     

    To remove it, you can add a MOUSE_UP event listener for the stage inside the function and in the event handler function for that new listener just use this.removeChild(newBio);

     
    |
    Mark as:
  • Currently Being Moderated
    May 8, 2012 2:58 PM   in reply to goodsnow82

    It would be much like your code for the button that you already have, except the stage would be assigned the listener instead of the button and the event would be a MOUSE_UP instead of a CLICK.  The function that you create for it would just need the one line I showed earlier.

     
    |
    Mark as:
  • Currently Being Moderated
    May 8, 2012 4:39 PM   in reply to goodsnow82

    You got very close except for a few things...

     

    You now have two functions named goBio, which is not going to sit well with the compiler.  Assign a different name for the new function. 

     

    Also, what was the only line I said you needed in the function for removing the movieclip?

     

    Lastly, you still haven't done the part you said you understood from my first response.  If you don't do it, your second function will not be able to target the movieclip

     
    |
    Mark as:
  • Currently Being Moderated
    May 9, 2012 12:40 PM   in reply to goodsnow82

    There is nothing for me to gain in writing your code for you, but there is for you.  If you go back thru everything I have offered and fix your code to what I describe you will not have no errors.  As it goes now, you have several and you have not done anything I have indicated you  should do except for assigning the stage an event listener correctly.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points