Cannot mask Stage3D SWF in Loader - actionscript-3

Working in FlashBuilder, I build a mobile AS3 application that uses a Loader to display a local SWF file. It masks the loader so it only shows a 640x480 window. This worked fine using an old SWF file (a Flixel game, non-Stage3D).
I then tried it with a Stage3D-enabled SWF file. This failed to run, because the application was not set to run in the 'direct' renderMode (it had been in auto up until this point). This allowed the application to run, but the SWF file now ignores the Loader's mask and displays across the entire stage.
Is it not possible to mask Stage3D SWFs when loaded in this way? The loading looks like so:
public function FlixelTest()
{
super();
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
myLoader.x = (stage.fullScreenWidth-640)/2;
myLoader.y = (stage.fullScreenHeight-480)/2;
var url:URLRequest = new URLRequest("stage3dswf.swf"); // in this case both SWFs are in the same folder
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
myLoader.load(url); // load the SWF file
addChild(myLoader);
}
private function loadProdComplete(e:Event):void{
var gameMask : Shape = new Shape;
gameMask.graphics.beginFill(0xffcc00);
gameMask.graphics.drawRect(myLoader.x,myLoader.y,640,480);
gameMask.graphics.endFill();
myLoader.content.mask = gameMask;
}

As you can read in Adobe's documentation on Stage3D, the special Stage3D layers are located "behind" the regular stage used for 2D content.
Since any mask applied within the 2D stage exists in a different display list, there is no way to use 2D masks on Stage3D content. If at all possible, the only way to get similar results is to use 3D layers and alpha masks within the Stage3D context.

Related

Converting MovieClip to Bitmap with Dispose()

I am working on a mobile game using Flash CS6 with AIR. also design and coding on same platform (not using starling etc.).
I am converting movieClips (static, not animated) to bitmap dynamically and its working fine. But i realize with this process bitmapData caching on memory and with big shapes it takes a lot of memory. Then i decide to after added to stage clear to bitmapData by dispose(). But its removes from stage and anywhere its shown.
My code;
var target:MovieClip = new Ex_mc2();
target.x=100;
target.y=300;
addChild(target);
var bounds:Rectangle = target.getBounds(this);
var bmpData:BitmapData = new BitmapData(Math.floor(bounds.width), Math.floor(bounds.height), true, 0);
var bmpMatrix:Matrix = target.transform.matrix;
bmpMatrix.translate(-bounds.x, -bounds.y); // Draw bitmap
bmpData.draw(target, bmpMatrix);
var bmp:Bitmap = new Bitmap(bmpData);
bmp.x=100;
bmp.y=300;
addChild(bmp);
removeChild(target);
//bmpData.dispose(); I want to use this and i dont want my bmp disappear
Searching to solution for one week but i cant figure it out.
So my question is;
Can i converting movieClips to bitmap with freeing to memory? Like adding stage a static png file?
BitmapData -- is actually what's being shown on the screen. The format is almost identical to BMP, but the order of bytes is reversed. Bitmap is only a display container, serving to output image content to the screen and providing common DisplayObject APIs.
So, if you use BitmapData.dispose(), you are actually freeing the memory occupied by image and obviously after that there is nothing to be shown by Bitmap container.

Bitmapdata.draw(MovieClip) Gives Blank BitmapData

I'm predrawing all vector element in my game to bitmaps to improve performance - the vector elements are exported as swf files from Illustrator (I suspect that is part of the problem).
When embedding the swf file in as3 like so:
[Embed(source = 'file.swf')] private static const image:Class;
And then adding it to the stage, like so
stage.addChild(new image);
Everything is fine, but if instead I draw it to a bitmap data like so...
genericBitmapData.draw(new image);
It's a blank bitmapdata. I've tried using gotoAndStop on frames 0 and 1 before drawing with no avail. Does anyone have experience with the swf files that Illustrator exports and any insight as to how I can get these drawing as expected?
Here is a link to a really simple .swf that produces the same results
Here's my revised solution:
//create movieclip
var mc:MovieClip = new image();
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
//this gets called on next frame
function enterFrameHandler(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
genericBitmapData.draw(mc);
}
So it turns out that the object needs to be instantiated before I can use it to draw, and the easiest way to detect when that is is to listen for the ENTER_FRAME event:
(new image).addEventListener(Event.ENTER_FRAME, functionToDrawImage);
And when that function is called, it will be ready to draw to a bitmapdata, and then all is right with the world.

Starling AS3 -- How To Use Bitmap Objects in SWC

I'm trying to see how Starling could benefit the applications I build using as3. My knowledge thus far of Starling is that it only uses bitmap objects not symbol objects. How do I take a BitmapData class and create an starling Image out of it.
This bitmap was a symbol in flash that was converted into bitmap. It then had its properties modified to export for as3
Say there is a Ship BitmapData class in a swc.
According to Starlings documentation of how it handles other bitmap like a png, I would think I should be able to do this.
var myShip:Image = Image.fromBitmap(new Ship());
This of course doesn't work.
fromBitmap() expects to receive a bitmap object but here you are passing in a BitmapData Class. And Image class requires a texture.
Also your syntax is slightly incorrect.
NB. There is Texture.fromBitmap and Texture.fromBitmapData.
Although I haven't tried accessing assets from a swc you could try:
var myShip:Image = new Image(Texture.fromBitmapData(new Ship());
Perhaps try to get the Image appearing first before attempting to access the swc bitmapData
var bd:BitmapData = new BitmapData(100, 100, false, 0xFF0000);
var myShip:Image = new Image(Texture.fromBitmapData(bd);
addChild(myShip); //should display a red square

Starling - load SWF image(vector) into a Image

I'm trying to convert an old project of mine to use Starling for some extra performance.
I've come to a problem when I'm loading my SWFs and trying to display them.
Here is the code I used before: (no Starling)
private function swfLoaded(e:LoaderEvent):void {
var spr:Sprite = new Sprite();
spr = e.currentTarget.content;
bitmapData.draw(spr ...);
}
The problem when I'm using Starling is that the currentTarget.content is a flash.display.displayObject.
cannot convert com.greensock.loading.display::ContentDisplay#90ffec1 to starling.display.Sprite
I want to find a way to "convert" the flash.display.displayObject into a starling Sprite.
I also want to be able to store the loaded swfs content into a array as a sprite.
Thanks in advance,
Tompa
You're overwriting spr with a different value immediately after you create it, for one thing.
After you do the bitmapData.draw() call:
var tex:Texture = Texture.fromBitmapData(bitmapData, false, false);
The new texture can then be used to create a Starling Image sprite.

Transfer a BitmapData object from an AS2 swf to a parent AS3 swf via SWFBridge

I need to transfer a BitmapData object created in an AS2 swf to an AS3 swf. I'm using gskinner's SWFBridge to establish a two way communication between both flash movies.
The AS3 movie loads the AS2 swf which works completely standalone and lets the user manipulate MovieClips and finally generate an image from the composition he creates. I need the AS3 movie to receive this image (bitmapData), do some fancy image processing stuff AS2 isn't able to do and send the new image back to the AS2 movie.
So here's the code
AS2 swf:
var userCompo_mc:MovieClip = container.createEmptyMovieClip("userCompo_mc",10);
var image:BitmapData = new BitmapData(userCompo_mc._width, userCompo_mc._height);
finalCompo.attachBitmap(image); // Just to make sure the final bitmap is right
image.draw(userCompo_mc, compo.title);
//Send the image to the AS3 movie
sb1.send("imageTransfer",image);
AS3 swf:
function imageTransfer(bitmapData:BitmapData, title:String):void
{
var bmp:Bitmap = new Bitmap(bitmapData);
this.addChild(bmp);
trace(title); // --> returns the right title
trace(bitmapData); // --> returns null
}
I think using something like copyPixel32(), saving everything into an array and then passing it to AS3 would do the trick but it's really a performance hog.
Also, I'm not allowed to convert the AS2 swf into AS3 code.
Any suggestions?
Thanks you!
It seems like the as2 movie adds some decorations/movieclips to a an image.
After that you draw it and send attempt to send it to as3.
Since looping though all the pixels is slow, as you mention, I imagine it's faster to draw the as2 content from the as3 movie.
e.g.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaded);
loader.load(new URLRequest('yourAS2Movie.swf'),new LoaderContext(true));
function loaded(event:Event):void{
var as2Clip:AVM1Movie = AVM1Movie(loader.content);
var bd:BitmapData = new BitmapData(as2Clip.width,as2Clip.height,false,0);
bd.draw(as2Clip);
addChild(new Bitmap(bd));
}
In this snippet the as2 content is drawn on load. In your case you would trigger/call a function that draws the as2 content via SWFBridge, after the as2 movie is ready/setup for what you need.
This works assuming you want to display the as2 content inside the as3 movie, which means you will load the as2 movie anyway. If not, either you load the as2 content, but don't add it to the display list, which means you'll be loading the as2 movie. Otherwise, you could try to save your final bitmap from the as2 movie using a server side language(like php for example), then trigger a function in the as3 movie, via SWFBridge, that will load the previously saved image.
HTH