Embed bitmap in ActionScript3 - actionscript-3

How can I embed a bitmap in Actionscript 3 and get the BitmapData?
public class MyGame extends Sprite {
[EMBED(source="Assets/helicopter1.png")] private static var BMClass:Class;
public function MyGame() {
var BM:Bitmap = new BMClass();
var BMData:BitmapData = new BitmapData(BM.width, BM.height);
BMData.draw(BM)
}
}
I've tried everything. If I ever try to instantiate the embedded class (new BMClass();) I get this error:
TypeError: Error #1007: Instantiation attempted on a non-constructor..
If I use
[EMBED(source="Assets/helicopter1.png")] private static var BMClass:BitmapData;
or something similar the BitmapData is null.
Edit:
So I figured out that the embedded data is null, but I can't figure out why. What did I do wrong in the embedding?

Looks like you are embedding correctly if you don't get an error transcoding. You should be able to get the bitmapData directly from the bitmap:
[Embed(source="picture.jpg")]
private var Picture:Class;
// create a bitmap of the embedded
var pic:Bitmap = new Picture();
// add to display list
addChild(pic);
// if you need to get the bitmapData for something else
var bitmapData:BitmapData = pic.bitmapData;

You don't need to instantiate as BitmapData and draw - you can simply:
[Embed(source="Assets/helicopter1.png")]
private var AssetClass:Class;
var bitmap:Bitmap = new AssetClass();

In some editors (at least my version of Intellij) the Embed tag is case sensitive. I got the exact same error you have when using [EMBED] but it worked great when I switched to [Embed]

Related

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 - Convert String to Bitmap

I'm trying to store my images on a server to lighten the load of my SWF file. I've spent all day trying to get the data from the server, but all I get is a seemingly random string (which I understand is the string value of my image). I've tried reading up on this, but I haven't found anything that actually works, I get a lot of references to Base 64 encoding/decoding and I've tried a couple of libraries but they do nothing. Here's what I have (I've simplified it a bit to upload it):
public function loadImage() : void {
var url:String='example.com/some_image';
var load:URLLoader=new URLLoader(new URLRequest(url));
load.addEventListener(Event.COMPLETE, onLoadComplete);
}
public function onLoadComplete(event : Event) : void {
var imgString:String=event.target.data;
}
But how do I convert imgString to a Bitmap or Sprite so I can addChild() it? I've tried imgString as Bitmap which returns null. Maybe it has something to do with the BitmapData class? This post seems similar but is unanswered.
If you have a server that runs PHP, you can generate the image first, e.g.
// PHP script 'http://yoursite.com/image.php
header("Content-type: image/jpeg");
echo $myImageDataFromDB; // Get the image data from MySQL.
And then in ActionScript, use the standard process to load that as an image:
var request:URLRequest = new URLRequest("http://yoursite.com/image.php");
var loader:Loader = new Loader();
loader.load(request);
addChild(loader);

How is this class connecting to the sound?

im trying to use (well succeeding) to use this sound class
http://www.mcfunkypants.com/2011/as3-pitch-shift-mp3/
the example code looks like this. . .
public class Pitch_Shift_Example extends Sprite
{
[Embed(source='Pitch_Shift_Example.mp3')]
private var engine_mp3 : Class;
public var engine_loop:Pitch_Shift_MP3;
public function Pitch_Shift_Example()
{
engine_loop = new Pitch_Shift_MP3(engine_mp3);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
var someText:TextField = new TextField();
someText.x = 200;
someText.y = 0;
someText.textColor = 0xFFFFFF;
someText.selectable = false;
someText.autoSize = TextFieldAutoSize.LEFT;
someText.text = "Pitch Shift MP3 Demo by Breakdance McFunkypants\nMove your mouse to change the sample rate. Enjoy!";
addChild(someText);
}
private function onMouseMove(event:MouseEvent):void
{
engine_loop.rate = (mouseX / stage.width) * 2;
}
}
Now what is confusing me is how the engine_mp3 class uses the sound.
line one embeds the sound.
line two creates an empty class called engine_mp3.
line 3 creates a new pitch shift class which in line 7 we pass the (empty) engine_mp3 class.
Basically how is engine_mp3 getting the mp3 data??
Any help is appreciated.
Andy
It works like something like this:
You embed the mp3 file with these two lines:
Embed(source='Pitch_Shift_Example.mp3')]
private var engine_mp3 : Class;
Those two lines work together. The first line is embed metadata that describes the line below it. What it's doing is embedding the mp3 file and making it accessible as a class. You therefore have a reference to this embedded sound class with the variable name engine_mp3.
Later on in your code, you instantiate a new instance of the Pitch_Shift_MP3 class, and you pass in the reference to your embedded sound class engine_mp3:
engine_loop = new Pitch_Shift_MP3(engine_mp3);
The constructor for the pitch shift class is expecting a class (that represents a sound) as its single argument. What it must be doing in its own code is instantiating the class that engine_mp3 represents by doing this:
var instantiatedSound:Sound = new engine_mp3() as Sound;
Hope that makes sense!

embedding image inside as3 using Flash

I'm using Flash Pro, CS6 on the Mac.
I've got this class
package classes
{
public class AssetEmbeds_1x
{
//kid
[Embed(source = '../graphics/Player_Graphics.swf')]
public static const playerG:Class;
}
}
Which is a swf which contains an exported (on 1st frame) movieclip called kidCharacter. I'm trying to access that kidCharacter movieclip from another class, and I'm not able to do that.
I've tried this...
var textureClass:Class = AssetEmbeds_1x;
var newClass = textureClass['playerG'];
myGraphic = newClass['kidCharacter'];
But it's not working, myGraphic (which is a movieclip) is always null. I'm not sure if it's even embedding anything.
Try this:
var player:MovieClip = new AssetsEmbeds_1x.playerG();
var kid:MovieClip = player.root.kidCharacter;

How to save a Image to PNG format?

Now Im saving to text format, and I got a error: TypeError: Error #1009: Cannot access a property or method of a null object reference.
at SaveImage/onClick()[/Users/VVT/Documents/Adobe Flash Builder 4.6/SuperDraw/src/SaveImage.as:40]
I wanna change my code so I can save to PNG format?
public class SaveImage extends Sprite
{
private var btnSave:buttonSave;
//private var ba:ByteArray;
private var file:FileReference;
public function SaveImage()
{
// Skapar min knapp.
var btnSave:buttonSave = new buttonSave();
addChild(btnSave);
btnSave.x = 400;
btnSave.y = 440;
btnSave.addEventListener(MouseEvent.CLICK, onClick);
var file:FileReference = new FileReference();
}
private function onClick(evt:MouseEvent):void
{
//var ba:ByteArray = file.encode(bitmapData);
//file.save(file);
file.save("some text. \nsome more text", "actionsnippet.txt");
}
}
You have a property named file, yet you are creating and initializing a local variable with the same name in this line of your constructor:
var file:FileReference = new FileReference();
Don't worry, those mistakes happen. Remove the var and type to get rid of that null reference error.
file = new FileReference();
To save the image as png, the as3corelib library, which is mentioned in this answer of the question linked in this comment, looks quite promising. Import the library and let it encode your bitmapdata:
file.save(PNGEncoder.encode(bitmapData));