How to capture current frame from a MovieClip into a BitmapData object? - actionscript-3

The movieclip is in a seperate swf file that has been imported into the library. The movieclip itself plays fine (the movieclip is vector based) but when I try and capture the current frame from it into a bitmapdata object nothing seems to happen. I'm wondering if it's some type of security issue?
mc is the movieclip
bitmap=new BitmapData(mc.width,mc.height,false);
trace("Creating bitmap for frame grab width=",mc.width,"height=",mc.height);
bitmap.floodFill(0,0,0xff0000); //for debugging only
bitmap.draw(mc);
After using the code above to capture the current frame of the movie the texture is solid red so it seems that it is not being changed after the floodfill command I put above for debugging.

Figured it out, the image is being drawn but is off the right of the screen since it seems to be using some sort of centerpoint or offset position to draw the movie. If I allocate the bitmap to be 4x taller and 4x wider then I see the bitmap.

Related

Dynamically resize a symbol using AS3 and then apply tween on the resultant symbol

I am able to use a classic tween to move a symbol on the stage ( frame 2 to frame 15) on stage layer. I apply AS3 command to resize the symbol ( a simple rectangle as movie) in frame 0 on AS3 layer. I issue stop() command at frame 15 on the AS3 layer. They work independently but the resultant resized symbol is not shown. Any help will be appreciated.

How do i create an animation in AS3?

I am making a game in AS3 for a school project (using the AIR api). I have about a year's worth of experience in AS3, so i would say i'm proficient but not an expert. Anyway, i have never attempted to do sprite animations in AS3 and i'm not quite sure how to approach it.
If i create a list of bitmaps and call addChild() and removeChild() to display each frame of the animation, it would affect the framerate as these functions are not very efficient (i have tried this before and it tanked my framerate if i had too many animations going at once). I have also tried creating a list of BitmapData objects, adding a bitmap to the display list, and then pointing it to a different BitmapData each frame, but this does not seem to work at all.
So what is the best way to do this? In XNA, for example, i would create a sprite class that draws to the screen using a sprite batch, then i would create a list of sprite objects and cycle through them to create the animation. Is there any way to achieve a similar result in actionscript?
First (simple) method: you can just use multi-frame MovieClip for animation. Put an image in each frame, put a MovieClip on the stage and that's all. You can control this animation using play(), stop(), gotoAndPlay(), gotoAndStop(). It will work without much problems for a generic platform game (I did that myself long ago).
Second (advanced) method: use bitmap blitting. For each animation, create a bitmap image that holds each frame of the animation. Use this image as a source for copying pixels into your current animated object. You just need to copy a particular rectangle area inside a source bitmap that corresponds to the current frame.
The actual blitting happens here
destinationBitmapData.copyPixels(sourceBitmapData, areaRectangle, destinationPoint);
Where destinationBitmapData is your "canvas" that you're blitting to; sourceBitmapData is the source image holding all animation frames; areaRectangle is the Rectangle inside the source image defining which area to copy; destinationPoint is left-top coordinate of the copy area in your canvas.
The destination canvas can be just one small object (like your game character that is moving around) or the entire game screen with all objects. I.e. instead of blitting and adding each object separately, you can just have one big canvas and blit any necessary parts directly to it.
That said, there is already a number of various ready-made engines that use blitting and even advanced techniques like 3D acceleration for 2D sprites.
One of them is Starling.

AS3 - looping through movieclips and rasterizing them to bitmapdatas - how to be sure frames are ready?

this is a newer version to a question i have asked before but have not received an answer to.
I am developing a mobile AIR app with many animations and tests have shown that using bitmaps and GPU rendering give the best performance across all mobile models. I receive normal vector animations from the app's artists, and have built a system which loops through these animations at runtime and draws the content into bitmapdatas.
my concern is looping through the movieclip's frames. If I have these lines of code:
for (var i:uint=1; i<mc.totalFrames+1; i++) {
mc.gotoAndStop(i);
bitmapData.draw(mc);
}
I can't be sure the frame got "constructed" before being drawn, and my tests with an Android tablet prove this right - sometimes frames aren't drawn.
This mc is off the display list obviously (we dont need to render to the screen). So is there a way to make sure the frame has been built before drawing it to a bitmapdata? (and waiting for FRAME_CONSTRUCTED, EXIT_FRAME, etc.. is obviously slow and unneeded)
any ideas?
thanx
Saar
more info to clarify:
this is a children's book. each page has animations in it. all these animations are vector animations in timelines in FLAs I receive from the devloping artists (not the users).
Every page has an FLA which i publish to a swf.
what i actually do is replace vector animations in loaded SWFs with bitmap version of them.
At app runtime, upon each new page, i load the swf, go though all the animations in it (looping through the content's children) and each movieclip i rasterize into array of bitmapdatas.
My custom "Bitmap Movieclip" places on the displaylist a replcaement bitmap for each movieclip, and on ENTER_FRAME i switch the bitmaps' bitmapdatas from the array.
this gives very hight performance
I'd suggest use a splash screen, and place your MC to be converted to the bitmap to the stage first, then draw to your bitmapData. Several issues had me force to put a DisplayObject being drawn to the stage before actual drawing once already, so this might be a quick fix for you too.
var splashBD:BitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,false,0xffffff);
var splashBM:Bitmap=new Bitmap(splashBD); // draw a thing as needed here
addChild(splashBM);
addChildAt(MC_to_convert,getChildIndex(splashBM));
// then advance it by frames and draw.
sorry MY BAD
waiting for FRAME_CONSTRUCTED is the answer

AS3 cacheAsBitmap questions?

I'm a bit confused. If I import a png picture, drag it onto my stage, and right click > convert to bitmap. Is that the same thing is if I have a vector created in code, and then apple cacheAsBitmap = true?
A PNG picture is a raster object, and it has a parent class of Bitmap already. You might, however, encapsulate that Bitmap into a drawn rectangle (effectively making a rectangle with bitmap fill, that is actually a vector object), and then convert to bitmap, applying cacheAsBitmap = true to the vector object made of the raster object. I don't understand why do you want double transformation raster->vector->raster in the first place. Probably Flash isn't so stupid if you just drag a library asset made out of a PNG to the stage, it'll make you a Bitmap based object instead, and "convert to bitmap" won't do a thing.

Clip staying on screen while not in timeline anymore

I have a particular problem : I have a fla with 2 frames, with a clip of instance name "my_clip" on frame 1 and no clip en frame 2. If i move by code (like a tween) "my_clip", when I go to frame 2, the clip is still on screen, whereas frame 2 is totally empty in my fla.
How flash work at this level ? Does he stop "syncronizing" a clip with the fla once the clip has been moved by code ? How can I check at frame 2 that the scene should be empty ?
(I'm with flash cs 5.5 and player 11)
Actually what FLASH does when we are trying to change the depth or start drag and some more dynamic functionality it freezes the target movieclip in stage. Eventually there is only one stage in FLASH and guess what if the movieclip has been frozen in the stage? It is visible or available always.
So in your case the same has happened. The clip has just frozen to the stage. Needless to say, the second frame is empty always. You don't need to check the second frame. What all you need to do is remove that clip before you navigates to either next frame or scene.
#LondonDrugs_MediaServices : I can't post my code and fla because there is too much code unrelated to the problem.
But as I was trying to make a little fla to reproduce my problem and show it to you, I figure out that the problem was not where I thought it was : I fact it was because each time I start dragging my clip i was doing a setChildIndex on it to put it on top. And it was precisely the setChildIndex wich would cut the "syncronization" between the clip and the fla timeline.
Thanks anyway for your answers, it kind of help me in a way :)