AS3 - How to animate all movie clips in the stage if i click a certain button - actionscript-3

Hi have been reading on this great website, here is what i want to accomplish,
on this certain page in the app im doing, there are movie clips (buttons, texts, menus) , now what i want is if i click this "close" button, i want everything to animate e.g shrink one by one. Question: Is there a great way to code this efficiently, and not coding it one by one e.g menu_mc.gotoAndPlay("out");, text_mc.gotoAndPlay("out"); image.gotoAndPlay("out"); ...something like that. Sorry im so new to this, any help will be greatly appreciated.

You could iterate the DisplayList, and call your method...
for (var i:int = 0; i < this.numChildren; i++) {
var obj:* = this.getChildAt(i);
if (obj is MovieClip) {
obj.gotoAndPlay("out");
}
}
Or you could animate with an animation library (such as Greensock)...
for (var i:int = 0; i < this.numChildren; i++) {
var obj:* = this.getChildAt(i);
TweenLite.to(obj, .35, {scaleX:0.5, scaleY:0.5, delay:0.25*i});
}
Or you could reference an Array for the objects you want to animate; a superior choice (if your objects aren't in the same container).
var a:Array = [menu_mc, text_mc, image];
for (var i:int = 0; i < a.length; i++) {
TweenLite.to(a[i], .35, {scaleX:0.5, scaleY:0.5, delay:0.25*i});
}

Related

Arranging items in an inventory

I'm working on an inventory system I made following a short tutorial that leaves you stranded. I've managed to get the items removed and rearrange to the correct order somewhat. For some reason though, if I click on the last item in my inventory, then on the first item, the items do not rearrange correctly.
public class Inventory {
var itemsInInventory:Array;
var inventorySprite:Sprite;
var itemNum:int;
public function Inventory(parentMC:MovieClip) {
itemNum=0;
itemsInInventory = new Array();
inventorySprite = new Sprite();
inventorySprite.x = 50;
inventorySprite.y = 360;
parentMC.addChild(inventorySprite);
}
public function makeInventoryItems(arrayOfItems:Array){
for(var i:int = 0; i < arrayOfItems.length; i++){
arrayOfItems[i].addEventListener(MouseEvent.CLICK, getItem);
arrayOfItems[i].buttonMode = true;
}
}
public function getItem(e:MouseEvent){
var item:MovieClip = MovieClip(e.currentTarget);
itemsInInventory.push(item);
inventorySprite.addChild(item);
item.x = (itemsInInventory.length-1)*40;
item.y = 0;
item.removeEventListener(MouseEvent.CLICK, getItem);
item.addEventListener(MouseEvent.CLICK, useItem);
}
public function useItem(e:MouseEvent){
var item:MovieClip = MovieClip(e.currentTarget);
itemNum = item.x;
inventorySprite.removeChild(item);
itemsInInventory.splice(item, 1);
sortInventory();
}
public function sortInventory(){
for(var i:int = 0; i < itemsInInventory.length; i++){
if(itemsInInventory[i].x > itemNum){
itemsInInventory[i].x -= 40;
}
}
itemNum=0;
}
}
I belive thats all the coding info I need to provide for help solving this mystery.
Also, a link to the game for testing. If you would like a link for a download of the game, please ask.
LINK
Instead of substracting 40px, just set their position again:
for(var i:int = 0; i < itemsInInventory.length; i++){
itemsInInventory[i].x = i*40;
}
Also, I did not even know that it is possible to give an object reference to the splice function, I would rather use:
itemsInInventory.splice(itemsInInventory.indexOf(item), 1);
And remove the event listener from the item when you delete it from the inventory in the useItem function.
item.removeEventListener(MouseEvent.CLICK, useItem);
EDIT:
With Flash Player 10, Adobe introduced the Vector class which is kind of the same as the Array class, but it can only store one data type. In your case it would be MovieClip or Sprite. The Vector class is singificantly faster and more developer friendly because you can see the help from the IDE when you are typing myVector[i].. I recommend using that instead, although there is nothing wrong with Array. It is just outdated a bit, but is helpful when you want to store more data types.
myVector:Vector.<MovieClip> = new Vector.<MovieClip>();

How would I code a statement that will choose 5 random frames out of 12 in Flash ActionScript 3?

I'm sure this question has been asked a million times in a million ways, but I would appreciate the help anyway. I am working on a Flash Mastermind clone and have a movie clip with 12 colored "pegs" and a "hole" image. How would I code a statement that will pick five random frames and not just the first five frames? I have the barest idea, but I'm not entirely sure if it's right:
var totalColors:Number = 12;
var maxColors:Number = 5;
var chosenColors:Array:
for(var i:Number = 1; i<totalColors; i++)
{
chosenColors[i] = Math.floor(Math.random()*totalColors)+1
}
Thanks a lot in advance for your help!
Please note that the movieclip has been rearranged since I wrote this; I moved the first frame - the "hole" to another layer.
EDIT: Pan has helped a lot since I first asked this question. To test it I decided to expand on my Bejeweled clone code. I added five more shapes to the original 7. At first I thought the line "newPiece.type = Math.ceil(Math.random() * chosenColors.length);" inside the loop, so I commented the quoted line, which was outside of the j loop and part of the original code, and replaced it with this:
newPiece.type = chosenColors[j];
I am very sorry if this seems elementary to some; I am not the strongest programmer, especially when making games. I am far better at ASP.NET and UI design, but game development has always appealed to me for some weird, possibly insane reason. Anyway, here is part of the method for creating a new jewel. The two for loops are pan's code for choosing seven random frames out of twelve. Unfortunately, the movie still picks the first seven frames from the movie clip.
//i goes through all of the possible colors and adds them to the temp array
for (var i:uint = 1; i <= newPiece.totalFrames; i++)
{
temp.push(i);
}
//j chooses seven colors out of the array of all possibilities
for (var j:int = 0; j < numPieces; j++)
{
//index is the frame that has been chosen randomly
var index:int = int(Math.random() * temp.length);
chosenColors.push(j);
chosenColors[j] = temp[index];
//remove the index
temp.splice(index, 0);
}
newPiece.type = Math.ceil(Math.random() * chosenColors.length);
Again, if I've confused anyone with my bad code to English translation, here is an image of my running game and its Jewel movieclip so you will hopefully see what I mean.
var totalColors:Number = 12;
var maxColors:Number = 5;
var chosenColors:Array = [];
var temp:Array = [];
for(var i:Number = 1; i <= totalColors; i++)
{
temp.push(i);
}
for (var j:int = 0; j < maxColors; j++)
{
var index:int = int(Math.random()*temp.length);
chosenColors[j] = temp[index];
//remove the index
temp.splice(index, 1);
}

ActionScript: How to start something that has a method: .stop( );

Alright, so I'm making this little game in ActionScript using Flash Builder. The duck asset has an animation. I used duck.stop(); to stop the Movie Clip so the animation doesn't play.
However, when I click on the duck, I need to figure out a way to once against start the Movie Clip. Does anyone know a way I could go about doing this?
private function makeDucks(amount:uint):void
{
for(var j:int = 0; j < amount; j++){
var duck:Duck = new Duck();
addChild(duck);
duck.x = j * (duck.width + duck.width / 3);
// .stop stops the MovieClip
duck.stop();
}
}
duck.addEventListener(MouseEvent.CLICK, onDuckClicked);
private function onDuckClicked(e:MouseEvent):void
{
var duck:MovieClip = e.target as MovieClip;
if(duck)
duck.play();
}

Order of instances in 3D Flash

I'm making cube of 9 cubes in Flash As3. However i cant rotate it properly due to order of indexes whole adding then to stage.
First im creating cube of 6 squares, then wall of 9 cubes, and at the end cube of 3 walls.
It is all fine, however when i rotate it to the left, order of cubes is inverted and it ruins whole composition. I know i coul dinamicly change index based on rotation but it would be a loooooooot of work.
Any ideas how could i do it better way?
Here is actual model:
http://test.mrowa.topdivision.pl/kostka/3DTest.html
If you are using the Flash's display list you would have to sort the sprites based on their z.
Here is some code that would sort the children of a DisplayObjectContainer based on their z position, call this whenever some object changes its position.
public function sortChildren(container:DisplayObjectContainer):void
{
var objects:Vector.<DisplayObject> = new Vector.<DisplayObject>;
for (var i:int = 0; i < container.numChildren; i++)
{
objects.push(container.getChildAt(i));
}
objects.sort(sortCompare);
var index:int = 0;
for (var j:int = 0; j < objects.length; j++)
{
index = container.getChildIndex(objects[j]);
if (index != j)
container.setChildIndex(objects[j], j);
}
}
private function sortCompare(a:DisplayObject, b:DisplayObject):int
{
return (a.z - b.z);
}
You can move the objects member to be a class member and add/remove items to it whenever you add/remove items to/from the stage so that you don't have to fill the whole array every time this function is called.

removing multiple movieclips

I'm developing a game.I've attached random movieClips from library. I've movieClips named picLeft1, picLeft2, picLeft3 and so on in library. Its working fine but i'm having problem removing it. the code below is for the attachment of movieclip. Here ranque is an array which stores randomly generated numbers up to 5 and HolderL is the movieClip in which I want to attach the movieClip. And q is a sprite.
for (var i:int = 0; int<3; i++) {
que_mc.push("picLeft"+ranque[i]);
var que_mc_class:Class = getDefinitionByName(que_mc[i]) as Class;
q = new que_mc_class();
this["HolderL"+(i+1)].addChild(q);
}
my code for removing the movieclip is given below.I want to remove all the movieclip attached in HolderL. but this code is not working.
for(var j:int = 1; j<=3; j++) {
this["HolderL"+j].removeChild(q);
}
Flash Player 11 allows you to use this:
for (var j:int = 1; j<=3; j++) {
var container : DisplayObjectContainer = this["HolderL"+j];
container.removeChildren();
}
Earlier versions don't have the removeChildren() command; you have to have two loops: One to loop to iterate over the container movie clips, one to traverse the display list and remove all children.
Shortest way to remove all children of a container prior to FlashPlayer 11:
while (container.numChildren > 0) container.removeChildAt(0);
It's not clear from your code what q is, but basically if you want to remove all the children from one movie clip, the simplest is to loop through all the children and remove them one by one. For example:
for (var i:int = container.numChildren - 1; i >= 0; i--) {
container.removeChildAt(i);
}