Getting BitmapData from Embedded class - actionscript-3

[ Embed('Assests/ui/Main.swf', symbol='backgroundImage')]
private var background01:Class;
private function getBitmapData(className:String):BitmapData
{
var mc:MovieClip = (new background01) as MovieClip;
if (mc == null) throw new Error('mc is null');
var myBitmapData:BitmapData = new BitmapData(mc.width , mc.height);
myBitmapData.draw(mc)
return myBitmapData;
}
Hi there. I'm having a problem working this out. I want to return bitmapData from a symbol that is embedded in a flash swf file. background01 is the class name exported to as3 in the flash swf file. As you can see from above I'm finding the swf with no problem. and I'm pullin the data back from the swf file with no prob. I can type addChild(new background01) and the image will show up. But when I've tried to cast to a bitmap , bitmapasset and movieclip. It always returns null. I want to get access to the bitmapdata of this image/movieclip. It should be returning a vertex circle drawn in flash and created to be a movieclip symbol. So what I would expect to be returned would be a MovieClip.
Has anyone ever done this, and if so, could you provided some sample code..
Thanks
LinX

Try:
var mc:MovieClip = (new background01()) as MovieClip;
instead of
var mc:MovieClip = (new background01) as MovieClip;

Related

as3 embed uploaded swf into scene

var file:FileReference=new FileReference();
and then
trace (file.data);
works fine
after that I'm trying to embed received data into the scene but with no success
var ExtSWF:MovieClip;
ExtSWF = file.data.readObject() as MovieClip;
trace(ExtSWF);
returns null
but if I load it as remote file, with Loader - it works fine
var ldr:Loader = new Loader();
ldr.load(new URLRequest("ext.swf"));
......
ExtSWF = MovieClip(ldr.contentLoaderInfo.content);
Is it possible to just upload swf file and embed it into the scene, or Loader class is the only possibility to archieve this goal?
The Loader class is used to load SWF files or image (JPG, PNG, or GIF)
files.
But "load" really meaning "decode format" for display. So pass your file.data bytes through the Loader using Loader.loadbytes for decoding to a valid MovieClip object.
Try
//var ExtSWF : MovieClip = new MovieClip;
//ExtSWF = file.data.readObject() as MovieClip;
var ldr : Loader = new Loader(); //# declare outside of any functions (make as public var)
ldr.loadBytes(file.data); //#use Loader to auto-decode bytes of SWF
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, on_swfLoaded );
And also have a handler function for the decoding completion...
function on_swfLoaded (evt:Event) : void
{
var ExtSWF : MovieClip = new MovieClip;
ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, on_swfLoaded );
ExtSWF = ldr.content as MovieClip;
trace(ExtSWF);
ExtSWF.x =50; ExtSWF.y = 50; addChild(ExtSWF);
}

as3 error: access of possibaly undefind property through a reference with static type flash.display:DisplayObject

I have this as3 project, and in frame one of the timeline I tried to load a swf movie named "menu" and in this loaded movie I have an instance of a button named "button1", and I want to add a new EventListener to this "button1". my code is here:
var theLoader:Loader = new Loader();
var address:URLRequest = new URLRequest("menu.swf");
theLoader.load(address);
theLoader.contentLoaderInfo.addEventListener(Event.COMPLETE , swfDidLoad);
function swfDidLoad(evt:Event){
if(theLoader.content){
addChild(theLoader);
var button:SimpleButton = theLoader.content.button1;
button.addEventListener(MouseEvent.CLICK, handler1);
}
}
function handler1 (event:MouseEvent):void
{
removeChild(theLoader);
gotoAndStop(10);
};
but I get this undefind property error. what should I do? Am i doing this right at all?
The reason you are getting that error is because you are trying to access button1 on theLoader.content which is a non-dynamic DisplayObject (this means that only explicitly defined properties/methods are valid). You must first cast it to a MovieClip (which is dynamic).
You should change that line to:
var button:SimpleButton = MovieClip(theLoader.content).button1;

AS3-embedded SWF comes up as null

So I have a SWF embedded in my AS3 project, but when I try to do anything with it, it says it's null.
The code looks like this:
[Embed(source = "../lib/Introduction.swf", mimeType = "application/octet-stream")]
public var introClass:Class;
(after a bunch of irrelevant stuff...)
var intro:MovieClip = new introClass() as MovieClip;
intro.play();
(The error message it gives me is a standard #1009 error.)
I've tried a bunch of stuff including using Loaders, not using MovieClip, etc, but at best, only the audio (not the video) of the SWF loads up, and at worst, the entire application crashes when the SWF tries to load. How do I get the SWF to be recognized?
(I'm using FlashDevelop if that helps.)
You can do this:
[Embed(source="asset.swf", symbol="symbol")]
private var symbolClass:Class;
var symbol:MovieClip = new symbolClass();
If you want to embed a symbol from an art SWF.
Instead if you want to import animations this is the solution:
[Embed(source="/loading.swf", mimeType="application/octet-stream")]
public var LoadingAnimation : Class;
// allows us to import SWFs to use as animations
var context : LoaderContext = new LoaderContext (false,
ApplicationDomain.currentDomain);
context.allowCodeImport = true;
var loader : Loader = new Loader ();
loader.loadBytes (new LoadingAnimation (), context);

AS3/FLEX - how to convert Sprite graphics into bytes

I wrote application that lets people paint together via internet (using Adobe cirrus). Everything works great but when for example I run my application and paint something before my friend connects, he dont see that what I have painted. So I'm looking for the method, that would let me convert my canvas into something(object) that it is possible to send by internet (I cant send whole Sprite, it's not possible to copy its graphics on the friend's application, it's null).
So let's get this clear. The main question is: How to convert graphic's of Sprite into object, that would let me convert it back to Sprite and copy its canvas.
ANSWER:
I used DisplayConverter library from "www.Flextras.com" post with his mod to convert Sprite to BitmapData and then to ByteArray and it works. I couldn't receive BitmapData on the friend's app, but It worked with ByteArray.
Sprite -> BitmapData -> ByteArray;
ByteArray -> BitmapData -> Sprite;
//TO SEND
var bitmapdata:BitmapData = DisplayConverter.spriteToBitmapData(palette);
var bytearr:ByteArray = bitmapdata.getPixels(bitmapdata.rect);
//TO RECEIVE
var bmd:BitmapData = new BitmapData(530,430);
bmd.setPixels(bmd.rect, bytearr);
mysprite.graphics.beginBitmapFill(bmd);
mysprite.graphics.drawRect(0,0,530,430);
mysprite.graphics.endFill();
Hope this will help someone
I think you want to convert your Canvas into a BitMap or BitMapData (and back). A Flex Canvas extends Sprite, so you can use a library like this one. To copy the relevant code, this will convert a Sprite to a BitMap:
public static function spriteToBitmap(sprite:Sprite, smoothing:Boolean = false):Bitmap
{
var bitmapData:BitmapData = new BitmapData(sprite.width, sprite.height, true, 0x00FFFFFF);
bitmapData.draw(sprite);
return new Bitmap(bitmapData, "auto", smoothing);
} // END FUNCTION spriteToBitmap
This will convert a Bit Map to a Sprite:
public static function bitmapToSprite(bitmap:Bitmap, smoothing:Boolean = false):Sprite
{
var sprite:Sprite = new Sprite();
sprite.addChild( new Bitmap(bitmap.bitmapData.clone(), "auto", smoothing) );
return sprite;
} // END FUNCTION bitmapToSprite
In my own development, I have a mod to this library, which allows me to get the BitMapData instead of an actual BitMap. So, this will turn a Sprite into BitMapData:
public static function spriteToBitmapData(sprite:Sprite):BitmapData
{
var bitmapData:BitmapData = new BitmapData(sprite.width, sprite.height, true, 0x00FFFFFF);
bitmapData.draw(sprite);
return bitmapData;
} // END FUNCTION spriteToBitmapData
This will take BitMapData and turn it back into a Sprite:
public static function bitmapDataToSprite(bitmapData:BitmapData, smoothing:Boolean = false):Sprite
{
var sprite:Sprite = new Sprite();
sprite.addChild( new Bitmap(bitmapData.clone(), "auto", smoothing) );
return sprite;
} // END FUNCTION bitmapToSprite
You do want to keep in mind that when converting the BitMap or BitMapData back into a Sprite you will probably not be able to cast it as a Canvas. For information on sending BitMapData to a server; look at this question.
A better approach is, instead of going directly to pixels, have the user gestures create data, then reflect that data as a drawing on your canvas. Transmit the same data to the other user and he/she will get the same drawing.

AS3 loading image from Array into empty movieclip

I have tried this to load image path from database via php and load the image in flash. Now how can i add child to every single image to a every single empty movie clip for the farther use. the code is
var ldr:URLLoader = new URLLoader(new URLRequest("m_shirts.php"));
ldr.addEventListener(Event.COMPLETE, _done);
function _done(e:Event):void {
ldr.removeEventListener(Event.COMPLETE, _done);
var ar:Array = String(e.target.data).split("#");
for (var x:int=0; x<ar.length; x++){
/*var img:Loader = new Loader();
img.load(new URLRequest(ar[x]));
shirts.addChild(img);*/
var myLoader:Loader = new Loader();
var my_mc:MovieClip = new MovieClip();
myLoader.load(new URLRequest(ar[x]));
my_mc.addChild(myLoader);
addChild(my_mc);
my_mc.x= my_mc.width + 50;
}
}
Ok, I will try to answer this from what I understand:
You simply cannot convert Loader instances to a MovieClip. A Loader is a Loader and a MovieClip is a MovieClip, much like an Array is an Array or a Function is a Function. Their common base class is DisplayObjectContainer. It has most of the functionality MovieClips have except for child manipulation (although the methods are there) and drawing API as per the graphics property.
You can access the size of the loaded image by reading the Loader's width and height.
Your problem seems to be, that you want to determine the image's size before it is loaded. You cannot do that. You have to wait for it to load first. The contentLoaderInfo will dispatch an Event.COMPLETE when the image is available.