Reputation: 49
i'm looking for a best approach to the following problem. I have an array that contains movieclips. All of the movieclips have te type and they are all positioned on the stage next to eachother.
Above the displayed movieclips i have 3 buttons, each simbolizing a type, what i want to do is: i click on one of the buttons and all of the movieclips that dont match the right type should be removed from the stage, the remaining movieclips should then tween so that they are next to eachother again. If i click the button again then the movieclips should be displayed again to where they were and should all be positioned again next to eachother.
What would be the best approach to handle this?
Upvotes: 0
Views: 81
Reputation: 1693
Alternatively, try using Array.filter.
public function onRedFilterClick(event:MouseEvent):void
{
var displayArray:Array = fullArray.filter(isRed);
display(displayArray);
}
private function isRed(element:*, index:int, arr:Array):Boolean
{
return (element.data == "red");
}
I've left out the implementation for display(). I usually use Greensock's TweenNano to tween elements.
Upvotes: 1
Reputation: 1456
You need to add some extra data to each MovieClip. Since data is dynamic, you can do this easily by adding some data to it like this:
myMovieClip.data = "red";
Then you could filter like so:
onRedFilterClick(event:MouseEvent):void
{
var n:int = theMovieClipsParent.numChildren;
while (n--)
{
var mc:MovieClip = theMovieClipsParent.getChildAt(n) as MovieClip
mc.visible = mc.data != "red";
}
}
Of course, there are stacks of ways to optimize this, like storing an array of MovieClips, storing an array just of red MovieClips... and so on. Or you could extend MovieClip so that you're not exploiting the dynamic class. But this should be enough to get you going. Note that theMovieClipsParent will need to be replaced.
As for tweening, well, that's a slightly separate issue. There are plenty of questions and answers about tweening. And there's also google...
Upvotes: 1