We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

AS3: How do I use button movieclips to load/unload other movieclips on stage?

Medium Priority
2,419 Views
Last Modified: 2013-11-12
Hi there experts!

I'm a newbie to AS3 and have limited experience in AS2. I'm having a tough time figuring out how to load/unload or addChild/removeChild movie clips dynamically on the main stage using AS3. The numerous tutorials I've come across on google seem to be using external .swf files when load/unloading and I've been able to get addChild/removeChild to work but can't figure out how to unload the previous movieClip before loading a new one. I apologize for the winded explaination.

I need some guidance on the best way to do this and how to make all this work in AS3. Here's what I've managed to create and have in my library.. please note, that I DO NOT have any movie clips on my stage and all instances (except the button movie clips) were set through the linkage property...

nav_mc (global navigation/instance name: nav) which contains:
  - home_mc (home pg/instance name: home)
  - about_mc (about pg/instance name: about)
  - sc_mc (services pg/instance name: sc)
  - mission_mc(mission pg/instance name: mission)

and a movie clip for each section of the site ....

homePg_mc
aboutPg_mc
scPg_mc
missionPg_mc

and because of this I have one keyframe on my main timeline with the following script. Now, how do i tell my buttons in nav_mc to unload or removeChild from the main stage before loading the new movie clip?


Any help or guidance would be greatly appreciated!!

Thanks,
SK

 
stop();
 
	var navigation:nav = new nav();
	var missionPg:mission = new mission();
	missionPg.x = 340;
	missionPg.y = 247;
	addChild(missionPg);
	navigation.x = 329;
	navigation.y = 158;
	addChild(navigation);

Open in new window

Comment
Watch Question


i am not sure if i have everything straight in my mind as to what your asking.
 
 But from what i gathered your trying to load Movie clips which have been exported for action script to the stage.
 
 what i would advise you to do is add all Movie clips to the stage. and then alternate the visible status of each as needed.
 
 this way the movie clips you don't need wont be visible or accessible until you set there visible status to true.
 
 please let me know if this helps.
 
 
 if possible could you upload a sample of the data file your working on so i can better understand what your trying to do better.
 
 Best of Luck
 
 GE

Author

Commented:
I know.. I'm sorry, I'm having difficulty explaining the issue since I'm not flash dabbler.. and yes, I'm trying to load/unload movie clips that have been exported for AS.

I'm attaching the .fla file for you if you are willing to look at it. I also organized the library/folder structure to make it easier for you and what you will see on the first frame of the main timeline is a script that does the following:

- loads the cover page (the intro) on to the stage
- from within the cover_mc symbol it will fire off an event from the enter button
- this refers back to the parent timeline and executes the missionPg function which first removes the coverPg movieclip from the stage then moves on with loading the mission and the navigation movie clips.

I hope this will help you better understand what I'm trying to do.
test2.txt
may i ask if there is any spacial reason why you don't have any instances on the stage?

Author

Commented:
No special reason... just that in the reading I've been doing (can't remember where now since I've been reading all sorts of articles, tutorials, etc.) it stated that by using "Export for Actionscript" and I have the ability to call the movieclip by class name and that I can them from the stage/timeline and just add them to the stage through code?? I take it that's not entirely true?

Author

Commented:
... I meant to say "...that I can delete them from the stage/timeline"
do you mind if i restructure your layout and scripts a bit?

GE

Author

Commented:
You are more than welcome to by all means.

That will actually help me in understanding how to implement a better timeline structure. I think that's also another thing I'm having a hard time understanding about AS3/Flash in general - is the layout and placement of symbols on the timeline.

I appreciate you helping me with this.
have a look at this.
to be honest i am not sure if this is the concept you had in mind for you project.

i liked some of the codes yo used however some of it may no be needed for a light project.

do you have experience with PHP?

GE


loadingMC2.txt

Author

Commented:
I'm glad you like some of the code I'm using... that tells me I'm doing SOMETHING right!! :)

This is only a sample that I'm working to put together so I can understand the concept. The REAL project I'll be working on involves 7-8 main movie clips that include 3 or 4 of their own nested clips.

and yes I do have some PHP experience.

   
i saw the php in the way you coded your script.

action script also uses cases  .
also you can use arrays to better access and reference data

GE

Author

Commented:
I see. I will keep that in mind.

Now, I'm disecting your code and structure to better understand...

I see where the code below will call up the mission movieclip if I click on the section2 button. But if I add the same code to the buttonClick3 function to access the about movieclip (about_mc), it will not remove/hide mission_mc on the stage. How do I tell my script to not overlap my new movieclip with the existing one on the stage.

So let's say basic user interaction is I click on "ENTER" which leads me to the navigation.. I click on MISSION, which takes me to the mission_mc animation, then I click on "ABOUT" and about_mc plays but it never unloads the previous clip (in this case mission_mc) from the stage . So regardless of whether I navigate from MISSION to ABOUT or ABOUT to MISSION the previous movieclip is still on the stage. How do I get it remove the previous clip from the stage dynamically?

CODE SAMPLE:
//MISSION
nav_mc.section2.addEventListener(MouseEvent.CLICK, buttonClick2);
nav_mc.section2.addEventListener(MouseEvent.MOUSE_OVER, buttonOut2);
 
//ABOUT
nav_mc.section3.addEventListener(MouseEvent.CLICK, buttonClick3);
nav_mc.section3.addEventListener(MouseEvent.MOUSE_OVER, buttonOut3);
 
//activates MISSION movieclip
function buttonClick2(e:MouseEvent):void {
	mission_mc.visible = true;
	mission_mc.gotoAndPlay(2);
	Tweener.addTween(nav_mc.section_mc, {alpha: 0, time: 10});
}
 
//activates ABOUT movieclip
function buttonClick3(e:MouseEvent):void {
	about_mc.visible = true;
	about_mc.gotoAndPlay(1);
	Tweener.addTween(nav_mc.section_mc, {alpha: 0, time: 10});
}

Open in new window

you don't need to remove it from the stage. just remove it from the stage display list.
that's what the visible setting is for. whichever clip your not using set its visible settings
to false thereby making it inactive for the user and its status removed from the display list.

GE

Author

Commented:
where would I apply that setting in my code?

How would I write it to tell it to remove the existing one from the stage so I don't have to hard code it for each function?

For example, if I wrote it this way, it would only look for mission_mc.. but what if the user is coming from home_mc. How would I tell it to set it to false dynamically?
//activates ABOUT movieclip
function buttonClick3(e:MouseEvent):void {
         mission_mc.visible = false;
	about_mc.visible = true;
	about_mc.gotoAndPlay(1);
	Tweener.addTween(nav_mc.section_mc, {alpha: 0, time: 10});
}

Open in new window

sorry i should have been clear.


when you Click one button such as about.
you would set all other buttons to false while setting about to true.

GE

disregard last post

when you Click one button such as about.
you would set all other movieClip or content to false while setting about us data to  true.

GE

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:

Wow. okay.. I thought maybe that's where addChild/removeChild might be able to help but I'm guessing not?

u can use that also. if you would prefer.
they are all options. its just how you want
to get things done.

GE

be sure to look at removeChildAt  and addChildAt.
these allow you to pic the order number the movie
clip is displayed on.

Author

Commented:
I see. I'm sorry for all the questions... I'm just trying to wrap my head around AS. So thanks for being patient with me.

I'm assuming that the advantages of each vary depending on the size and complexity of the project.
 

Author

Commented:
Outstanding. I will look into all of the great tips you've offered up through this thread.

Thank you so much for your time, guidance and assistance!! I appreciate it.

Is there a good "for beginners" resource such as a book, tutorial, etc. that you know of which helps someone understand the core fundamentals of AS3?

 
i got some good info from

cost:
lynda.com
cartoonsmart.com

free
http://actionscript.org/

youtube also has a few starter videos for as3.

also if you have the cash check out amazon.com for the as3 bible.( keep in mind its not a direct learning guild it as the name suggest a guide throw as3 and all its many mysteries)
they have other good books also. read some of the reviews they can be helpful.

Best of luck

GE

P.S Good luck in AS3 its been a great time for me so far

Author

Commented:
Thanks greeneel!!

I'm sure it will be equally fun and frustrating going through the process!
I will be sure to check out all of the resources.

Thanks AGAIN!!! You've been a great help!

SK

Author

Commented:
I'm truly great for the help! I hope someday I can return the favor.
You went above and beyond what I was expecting!! Thanks again!!

SK
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.