• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

closing a movie clip with EventListener with a mouse click

Guest
May 08, 2012 May 08, 2012

Copy link to clipboard

Copied

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;

}

TOPICS
ActionScript

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 08, 2012 May 08, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 08, 2012 May 08, 2012

Copy link to clipboard

Copied

I got the first part but I have no idea what the AS should look like for that second part to close the mc?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 08, 2012 May 08, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 08, 2012 May 08, 2012

Copy link to clipboard

Copied

So how would you assign the listener to the stage?

This is my code now:

btn_2.addEventListener(MouseEvent.CLICK, goBio);

function goBio (e:MouseEvent):void {

var newBio = new BioToc();

this.addChild(newBio);

newBio.x = 25;

newBio.y = 45;

}

stage.addEventListener(MouseEvent.MOUSE_UP, goBio);

function goBio (e:MouseEvent):void {

var newBio = new BioToc();

this.removeChild(newBio);

newBio.x = 25;

newBio.y = 45;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 08, 2012 May 08, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 09, 2012 May 09, 2012

Copy link to clipboard

Copied

If I remove var from this line var newBio = new BioToc(); then it stops working all together. So I'm not sure what you mean by just changing it to newBio = new BioToc();

I'm not sure what you mean by changing goBio, can I just put any word in front of Bio to change it?

This is what I did and now I get an error that says "Scene 1, Layer 'actions', Frame 1, Line 10. 1084: Syntax error: expecting leftparen before this.

btn_2.addEventListener(MouseEvent.CLICK, goBio);

function goBio (e:MouseEvent):void {

var newBio = new BioToc();

this.addChild(newBio);

newBio.x = 25;

newBio.y = 45;

}

stage.addEventListener(MouseEvent.MOUSE_UP, removeBio);

function removeBio this.removeChild(newBio);

var newBio = new BioToc();

newBio.x = 25;

newBio.y = 45;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 09, 2012 May 09, 2012

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines