views:

1791

answers:

4

How do you reference a bitmap on the stage in flash using actionscript 3?

I have a bitmap on the stage in flash and at the end of the movie I would like to swap it out for the next in the sequence before the movie loops. in my library i have 3 images, exported for actionscript, with the class name img1/img2/img3. here is how my layers in flash are set out.

layer 5 : mask2:MovieClip
layer 4 : img2:Bitmap
layer 3 : mask1:MovieClip
layer 2 : img1:Bitmap
layer 1 : background:Bitmap

at the end of the movie I would like to swap img1 with img2, so the movie loops seamlessly, then ideally swap img2 (on layer 4) with img3 and so on until I get to the end of my images.

but I can not find out how to reference the images that have already been put on the stage (in design time), any one have any idea of how to do this?

The end movie will hopefully load images dynamically from the web server (I have the code for this bit) and display them as well as img1/img2/img3.

Any help would be appreciated.

EDIT: @81bronco , I tried this but the instance name is greyed out for graphics, it will only allow me to do it with movieclips and buttons. I half got it to work by turning them into moveclips, and clearing the images in the moveclip out before adding a new one (using something simpler to what vanhornRF suggested), but for some odd reason when the mask kicks in the images I cleared out come back for the mask animation.

+1  A: 

It should be something like this:

imageHolder.removeChild( imageIndex )

or

imageHolder.removeChildByName( imageName )

and after that

imageHolder.addChild( newImage )
bitbonk
+2  A: 

To reference something on the stage, you need to give the stage instance a name - not give the symbol in the library a class name.

Click on the item on the stage and look at the properties panel. There should be a text entry box just above the entry boxes for the item's dimensions. Enter a name there.

Elsewhere in your code, you can then refer to that item on stage by it's instance name.

81bronco
A: 

I would probably do something like this in your document class

for(var i:int=0; i<numChildren; i++){
    trace(getChildAt(i),"This is the child at position "+i);
}

I do this because I still code in the flash IDE and its debugger is so very painful to get working most of the time it's easier to just trace variables out, so you can either use that for loop to print the object names of the items currently on your stage, or use a debugger program to find the objects as well.

Now that you have the children and at what index they actually are at within the stage, you can reference them by calling getChildAt(int), you can removeChildAt(int), you can addChildAt(displayObject, int) and swapChildrenAt(int, int). The int in these arguments would represent the index position that was returned by your trace statement and the displayObject would obviously just represent anything you wanted to add to the stage or parent DisplayObject.

Using those 4 commands you should be able to freely re-arrange any movieclips you have on stage so that they will appear to transition seamlessly.

@81bronco One should definitely name your assets on stage if you want to uniquely reference them specifically to avoid any confusion if there ends up being a lot of items on stage

vanhornRF
A: 

Hey Re0sless, when you remove those items from the stage do they have any event listeners attached to them, any timers or loaders? Any of those things can make an object stick around in flash's memory and not remove properly. Also on top of just removing the item, perhaps try nulling it as well? Sometimes that helps in clearing out its references so it can be properly destroyed.

Of course it could also be something silly like removing the item at one instance doesn't remove the item from future frames as well, but I really don't think that's the case.

vanhornRF