Accessing pieces of bitmaps inside a movieclip - actionscript-3

I have a movieclip with 10 bitmaps in it. I wanna access each of them.
myMovieClip is the movieclip containing those 10 bitmaps. I wanna access those bitmaps one by one. All 10 bitmaps are imported separately.
I tried this :
for ( var i =0 ; i< myMovieClip.numChildren ; i++)
{
trace ( myMovieClip.getChildAt(i) );
}
Problem is numChildren comes "1" only, as if it doesnot consider those 10 pieces of bitmap. Any other way, to access those bitmaps ?
thanks
V.

what do you mean by pieces of bitmaps?? Do you mean 10 different bitmap objects are children of the movieClip??
In addition, your code does have a syntax error.
var newMc:MovieClip = MovieClip();
should be:
var newMc:MovieClip = new MovieClip();
second off all, in your loop, numChildren will be always changing since you are taking the reference of a child from the myMoiveClip and moving it to the newMc object. there are two way to fix this.
either set a local variable to the value of myMovieClip.numChildren and use that value in your loop
example:
var numOfChildren:int = myMovieClip.numChildren;
for(var i:int = 0; i < numOfChildren; i++){
var newMc:MovieClip = new MovieClip();
newMc.addChild(myMovieClip.getChildAt(i));
}
this will move the bitmaps out of myMovieClip and into newMc, if you want to keep them there you can create a new bitmap inside the loop and then add the new bitmap to the newMc.
example:
for(var i:int = 0; i < myMovieClip.numChildren; i++){
var newMc:MovieClip = new MovieClip();
var b:Bitmap = new Bitmap(Bitmap(myMovieClip.getChildAt(i)).bitmapData);
newMc.addChild(b);
}

Related

Drawing BitmapData to Sprite still using bitmapdata itself?

I'm calling images from library to be use. But because I will be constantly add and remove the images, so I tried to copy the bitmapdata to a sprite and reuse the sprite.
private function loadImage():void {
for (var i:int = 1; i < typeAmount + 1; i++) {
SlotClass = Main.queue.getLoader('main_uiMC').getClass('slot' + i + 'bmp') as Class;
bmpDSlot = new SlotClass() as BitmapData;
bmpDSlotV.push(bmpDSlot)
}
}
private function bitmaping():void {
for (var i:int = 1; i < typeAmount + 1; i++) {
slotS = new Sprite()
slotS.graphics.beginBitmapFill(bmpDSlotV[i - 1], new Matrix, false, true)
slotS.graphics.drawRect(0, 0, bmpDSlotV[i - 1].width, bmpDSlotV[i - 1].height);
slotS.graphics.endFill();
bmpV.push(slotS)
}
Every time I duplicate the sprite, flashdevelop's profiler showed that the bitmapdata is being added as well. Even when I use removeChild to remove the Sprite, the memory usage won't decrease.
Is there a way to better copy the content of the bitmapdata, and can be completely remove when I remove the sprite?
*i will still be using the image, just that on that particular round i would like to remove the sprite that has the image.
You should use a single BitmapData object, create numerous Bitmap objects and operate these.
private function bitmaping():void {
for (var i:int = 1; i < typeAmount + 1; i++) {
slotS = new Bitmap(bmpDSlotV[i-1]);
bmpV.push(slotS);
}
Creating another Bitmap like this does not add another BitmapData, it uses existing one as reference.
Well your choice is to reuse the BitmapData. And because you need multiple instances of the same image, you need to create new one for each object that you would like to add. And this would be a Bitmap with the old BitmapData:
var originalBMD:BitmapData = instantiateItSomehow();
var first:Bitmap = new Bitmap(originalBMD);
var second:Bitmap = new Bitmap(originalBMD);
This way you would reuse the BitmapData and I think the memory will increase only because of the Bitmap objects, but the data itself should not be duplicated.

AS3 Best way to declare variables inside loop

Is it possible better to declare variables without having to write one by one?
I need 200 but... no way.
My code i have:
var var0:Array = new Array();
var var01:Sprite = new Sprite();
var var02:Sprite = new Sprite();
var var03:Sprite = new Sprite();
etc to 200...
And inside for loop:
for( var i:Number = 0; i < arrayBig.length; i++ ){
var0[i] = new Sprite();
...
}
But Why cant I write var var0[i]:Sprite = new Sprite(); in the statments?
The error is Error #1086 Syntax error: expecting semicolon before
Thank you very much!!
You cannot index independent variables and you cannot concatenate variable names to form names of other variables (well, not the way you attempted).
You need to create an array of Sprite objects. An array is a data structure that stores several things at once. (A one-dimensional array is called a vector.)
To store sprites in a vector, you'd write:
var sprites:Vector.<Sprite> = new Vector.<Sprite> ();
for ( var i:int = 0; i < 200; i++ )
{
sprites.push ( new Sprite () );
}
This loop creates 200 Sprite instances and stores all of them in the sprites array (really, Vector). You can then access individual sprites by simply indexing them:
sprites[n]....
where n goes from 0 to N-1 if the Vector has N total elements.
i will answer for your question.
"But Why cant I write var var0[i]:Sprite = new Sprite(); in the statements?"
we cant override one object with that of the other same object in the class. thats the reason, we are declaring as "var0[i] = new Sprite();". spl case for sprite object in flex.

AS3 reference movieclip by .name property

Yay, another simple noobie as3 question.
How can I reference a movieclip by its ".name" ?
I tried searching for a solution, but I couldn't find anything. Basically I have a set of movieclips added to the stage using a loop, so the way I found to diferentiate them was by giving them a .name of "something" + the Loop's "i". So now they are named something like "something1", "something2", "something3", and so on.
Now, I need to send some to a specific frame. Usually I would do something like:
something1.gotoAndStop(2);
But "something1" isnt the instance name, just the ".name". I cant find a way to reference it.
you want to use getChildByName("name") more info
import flash.display.MovieClip;
// create boxes
for(var i:int = 0 ; i < 4; i++){
var box:MovieClip = new myBox(); // myBox is a symbol in the library (export for actionscript is checked and class name is myBox
box.name = "box_" + i;
box.x = i * 100;
this.addChild(box);
}
// call one of the boxes
var targetBox:MovieClip = this.getChildByName("box_2") as MovieClip;
targetBox.gotoAndStop(2);
Accessing things by name is very prone to errors. It's not a good habit to get into if you're a newbie. I think a safer way to do this would be to store references to the things you're creating in the loop, for example in an array, and reference them by their indexes.
Example:
var boxes:Array = [];
const NUM_BOXES:int = 4;
const SPACING:int = 100;
// create boxes
for(var i:int = 0 ; i < NUM_BOXES:; i++){
var box:MovieClip = new MovieClip();
// You can still do this, but only as a label, don't rely on it for finding the box later!
box.name = "box_" + i;
box.x = i * SPACING;
addChild(box);
// store the box for lookup later.
boxes.push(box); // or boxes[i] = box;
}
// talk to the third box
const RESET_FRAME:int = 2;
var targetBox:MovieClip = boxes[2] as MovieClip;
targetBox.gotoAndStop(RESET_FRAME);
Note, I've also replaced many of the loose numbers with constants and vars which will also help your compiler notice errors.
You can use the parent to get the child by name. If the parent is the stage:
var something1:MovieClip = stage.getChildByName("something1");
something1.gotoAndStop(2);

How to Create Sound Object Dynamically

How to create sound object dynamically in one movieClip.
Example
for(i=1;i<5;i++){var sound + i = new Sound();}
You can try putting all the sounds from your loop inside an Array:
var soundArray:Array = [];
for (var i:uint = 0; i < 5; i++) {
var sound:Sound = new Sound();
// don't forget to set the path of the file you want to play
soundArray.push(sound);
}
To play the the sound, all you need to do is take note of the index:
Sound(soundArray[0]).play();
Hope this helps.
irot
you need a MovieClip to store your sounds into then:
var mc:MovieClip = new MovieClip();
for( var i:int = 0; i < 5; i++ )
{
mc[ 'sound_' + i ] = new Sound();
}
to access a sound, you can then call:
mc[ 'sound_0' ].play();
if you're already in the scope of a MovieClip, ignore the mc creation and replace 'mc' with 'this' in the loop.

Dynamically creating movieclip instances

I use the code below to create 3 movie clips.
var A:Array = new Array();
for (var i:uint = 0; i < 3 ; i++) {
A[i] = new hayvanSec();
A[i].x = 240+i*160;
A[i].y=300;
addChild(A[i]);
}
I have 10 image files in the library. I want to show one of these images inside those dynamically created movie clips randomly.
In the flash IDE make those image a MovieClip and name your clip with something like that XXX_0, XXX_1,...,XXX_9.
Then you can in your function you can get the random reference to your clip with :
var myImageName:String="XXX_"+Math.floor(Math.random()*10);
// and then get the movie from the library
var clazz:Class=ApplicationDomain.currentDomain.getDefinition(myImageName) as Class;
if (clazz !== null) {
var mc:MovieClip=MovieClip(new clazz());
(...).addChild(mc);
}