View Full Version : Help with understanding array of movieclips
ofeargall
October 30th, 2008, 04:39 PM
I'm new to AS3 and I need some wisdom.
I hope to have 4 movieclips in my library and buttons on the stage that act as a next/previous user interface. How do I set up my array to know the movicelip names and how do I get my buttons to select the next or previous movieclip in the array? I'm also trying to figure out how to keep the buttons disabled while the selected movieclip is playing.
My uneducated guess is that I need to create the array with all the movieclip names in it. Then put some addChild and removeChild code in when the buttons are clicked. Does this sound right?
I know there are a lot of questions here so I appreciate any help you all can provide, even if it's just addressing one question.
RichSad
October 31st, 2008, 12:48 AM
One solution is to use an Object instead of an array. An array in AS3 is for integer based, non-sparse arrays. The Object works like an "associative" array in PHP or a hash map in Java/C++. You could do something like this:
var movieList:Object = new Object();
movieList[mc1.name] = mc1;
movieList[mc2.name] = mc2;
...
if the "name" property of the movieclips aren't set to anything useful, use your own names as in:
movieList["mc1"] = mc1;
then you could access the desired movie by saying:
addChild(movieList.mc1); // or movieList["mc1"]
you can use movieList.hasOwnProperty("mc1") to test if the movieList contains a specific key
Hope this helps. The trick is to think Array when you have sequential integer indices and to think Object when you want to use strings as keys.
ofeargall
October 31st, 2008, 11:51 AM
RichSad,
Thanks for the info. I think the trouble with being new to something is not knowing what you don't know to ask about. Array vs Object is a perfect example. I feel like I can research a little more intelligently for tutorials and helps now.
Regarding the naming of the Objects as a list, now that I've given the clips meaningful integer based names I should be able to create loop based concatenated code for the previous/next buttons that load the clips, right?
Thanks again for your help.
RichSad
November 11th, 2008, 08:47 PM
ofeargall, I am not sure exactly what you are asking. If you want to use integer values to relate to the multiple movieclips it would be easiest to use a traditional array with integer indices. But if you want to use string-based indices that happen to end in sequential integer values, you can use string concatenation to create the key strings. Assuming we are using "mc1", "mc2" and so on, you can build the names by doing something like this:
const MAX:int = 5; //the highest integer value you want to use
for (var i:int=1; i<=MAX; i++) {
var nameOfMovieClip = "mc" + i;
trace(nameOfMovieClip);
}
given that this is fairly obvious I am guessing I don't really understand what you want to do.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.