as3 PNGEncoder error - actionscript-3

I am trying to save my graphics using the PNG encoder, and I get two errors.
Scene 1, Layer 'canvas', Frame 2, Line 42, Column 12 1172: Definition PNGEncoder could not be found.
Scene 1, Layer 'canvas', Frame 2, Line 965, Column 24 1120: Access of undefined property PNGEncoder.
Here is all the code revolving saving:
import PNGEncoder; [Recieves the first error]
function export():void
{
var bmd:BitmapData = new BitmapData(board.width, board.height);
bmd.draw(board);
var ba:ByteArray = PNGEncoder.encode(bmd); [Recieves the second error]
var file:FileReference = new FileReference();
file.save(ba, "MyDrawing.png");
}
saveButton.addEventListener(MouseEvent.CLICK,save)
function save(e:MouseEvent):void
{
export();
}
I got this code from a tutorial, so I can't be too sure it is formated right. The tut said that PNGEncoder was an import but according to flash cs3 it is not. Can someone help me straighten this out? I labeled the parts giving me an error, thanks.

You need to download the PNG Encoder and setup Flash to know where to find the new library (the process below can be used for any other externally downloaded SWC's you need to use in future)
1) Create a new folder called Flash_Addons
(the path would be C:\Flash_Addons\ it's just to get you started for now, later change the setup to a prefered folder name/location)
2) Download the AS3 CoreLib (the "Download ZIP" option at bottom/right-hand side
3) Inside the Zip is a folder (I think its called "as3corelib-.93") copy that to your Flash_Addons folder so it's now a sub-folder.
4) Now to tell Flash where to find it.. In your CS got to Edit-->Preferences (CTRL+U shortcut) and click on "ActionScript" in Category list then when shown click ActionScript 3.0 Settings
Now you will see three boxes (Source path/ Library path/ External path).
(for these below, click "+" to add new entry, then either type path or click the folder icon to browse)
Source path: You want it to add.. C:\Flash_Addons\
Library path: You want it to add.. C:\Flash_Addons\as3corelib-.93\lib
(note: the "lib" folder contains the actual as3corelib.SWC file, that's why we put direct path to it. When you want to add any new SWC library just add path to the list of entries (example):
C:\Flash_Addons\as3corelib-.93\lib
C:\Flash_Addons\Useful_Library_Folder_of_SWC_file\
etc... etc..
Now you're ready to export PNG..
import com.adobe.images.PNGEncoder; //fixes the errors
saveButton.addEventListener(MouseEvent.CLICK, save)
function save(e:MouseEvent):void
{ export(); }
function export():void
{
var bmd:BitmapData = new BitmapData(board.width, board.height);
bmd.draw(board);
var ba:ByteArray = PNGEncoder.encode(bmd);
var file:FileReference = new FileReference();
file.save(ba, "MyDrawing.png");
}

Related

AS3 HMAC SHA-256 for Adobe AIR (desktop)

in Actionscript 3 i need to compute data (a string, utf-8), using a secret key (a string, utf-8).
This is the data (string) "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559"
This is the secret key (string) "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"
This is the result it must come out (string) "c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71"
Can someone solve this? (the result is correct, but i don't know the code to arrive at the result).
This is what i have tried so far:
// THE DATA
var dataToEncode:String = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559";
var byteArrayOfDataToEncode:ByteArray = new ByteArray();
byteArrayOfDataToEncode.writeUTF(dataToEncode); // we write the string into the ByteArray
// THE SECRET KEY
var secretKey:String = "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j";
var byteArrayOfSecretKey:ByteArray = new ByteArray();
byteArrayOfSecretKey.writeUTF(secretKey); // we write the secret key into the ByteArray
// WE COMPUTE THE SIGNATURE
var HMAC_SHA256:HMAC = new HMAC(new SHA256());
var byteArrayOfResultSignature:ByteArray = HMAC_SHA256.compute(byteArrayOfSecretKey, byteArrayOfDataToEncode);
var resultSignature:String = byteArrayOfResultSignature.readUTF();
// WE SHOW THE RESULT SIGNATURE
trace("The result signature is: "+resultSignature);
This HMAC class is created using this library (AS3Crypto):
https://github.com/Atmosphere/ActionScript/blob/master/src/com/hurlant/crypto/hash/HMAC.as
I found, thanks to the commentors, the asnwer to this.
The solution is com.adobe.crypto .
There is a class in the As3corelib (that you can download here
https://code.google.com/archive/p/as3corelib/downloads). You have to import those folders by going to "File-> Actionscript settings", and the click on the source path and then add the "com" folder there.
You will also need to download the Adobe Flex SDK (because some classes in As3corelib use some utility classes in the Flex SDK), unzip it, and then include the framework.swc file (search in the folders of adobe flex, it is in the frameworks/libs folder), going to "File-> Actionscript settings", and the click on the library path and then add the framework.swf there.
Now you are ready to do it, like this:
import com.adobe.crypto.*; // import this, to have access to the classes we need.
// then this code where you need it
var message:String = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559";
var secretKey:String = "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j";
var signature:String = HMAC.hash(secretKey, message, SHA256) ;
trace("The signature is: "+signature);
Hope it helps, have a beautiful day!

Saving customized movieclip as an image to local disk (diy generator)

dropbox.com/s/77euop1luqjreos/FINAL.fla
Ok i just gave up with this already. I cant think of any way to save the image created by a user. Its hard to explain but please check out the fla file of my work. Basically its a diy generator. The only thing missing is a save function. Ive read filereference but its always in a document class. My code is in the timeline. Please help I'm really stuck.
edit: I got a download button that is working now!! But it only saves a small part of the movieclip: Imgur
In order to create images, you need to access the pixel data of the MovieClip object. The data can be obtained by rendering a MovieClip into a BitmapData object and using this data, you can write your own encoder to convert it to any image format you’d like. Writing such encoder is not a trivial task and requires understanding of the image format algorithm, or you can use pre-written libraries. You can download the PNGEncoder and JPGEncoder , which is part of as3corelib, an open source project library.
Using the JPGEncoder, we can convert DisplayObject into ByteArray, suitable for saving into file. If using the JPGEncoder, it’ll look like this:
import com.adobe.images.JPGEncoder;
var jpgEncoder:JPGEncoder = new JPGEncoder(quality);
//remember bitmapData here is just an example (do not try to compile this code without declare this particular variable)
var byteArray:ByteArray = jpgEncoder.encode(bitmapData);
With the PNGEncoder, it’ll look like this:
import com.adobe.images.PNGEncoder;
//remember bitmapData here is just an example (do not try to compile this code without declare this particular variable)
var byteArray:ByteArray = PNGEncoder.encode(bitmapData);
Saving Into User’s Hard Drive
Using the FileReference.save() function, we can prompt the user to save the file with the following call.
var fileReference:FileReference=new FileReference();
//in case of JPGEncoder
fileReference.save(byteArray, ".jpg");
With the two combined, here’s an example how to use:
//remember to import
import flash.net.FileReference;
import com.adobe.images.JPGEncoder; //or import com.adobe.images.PNGEncoder;
import flash.utils.ByteArray;
import flash.display.BitmapData;
//where mc_canvas will be your MovieClip instance name
var bitmapData:BitmapData = new BitmapData(mc_canvas.width, mc_canvas.height);
bitmapData.draw(mc_canvas);
var jpgEncoder:JPGEncoder = new JPGEncoder(quality_slider.value);
var byteArray:ByteArray = jpgEncoder.encode(bitmapData);
//if you want to use PNGEncoder
//var byteArray:ByteArray = PNGEncoder.encode(bitmapData);
var fileReference:FileReference = new FileReference();
fileReference.save(byteArray, ".jpg");

Unable to load SWF in as3

I am working in flash and as3. I am new to as3. I was trying to load and unload the SWF file.
My project contains 2 files, one is index file and the other is animal file. In the index page I have button for animal. When I click on this button the animal SWF starts executing. But the problem is when I click on index button of animal SWF, it is not showing index page again, instead it is showing message as
Unable to load SWF
My index page code is:
var urlReq:URLRequest = new URLRequest("animal/animal.swf");
swfLoader.load(urlReq);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadComplete);
swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,
swfLoadError);
function swfLoadComplete(evt:Event):void
{
var loader:Loader = Loader(evt.target.loader);
addChild(loader.content);
swfLoader.removeEventListener(Event.COMPLETE, swfLoadComplete);
}
function swfLoadError(evt:IOErrorEvent):void
{
trace("Unable to load swf ");
swfLoader.removeEventListener(IOErrorEvent.IO_ERROR, swfLoadError);
}
So what to do to load the index SWF from animal SWF?
You probably forgot to add to the displaylist. Use addChild.
var urlReq:URLRequest = new URLRequest("animal/animal.swf");
swfLoader.load(urlReq);
this.addChild(swfLoader);
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html
Update: I see its not loaded. You have an IOErrorEvent.IO_ERROR. Mostly this means the file is not located at that url. Check the location or use a http debugger to find out which location you are trying to open.

How to get all definitions in an ApplicationDomain of a loaded SWF?

When you load a SWF into another, the loader SWF can get specific definitions from the loaded SWF using ApplicationDomain.getDefinition(name:String). For example:
package
{
// ... imports
public class SWFLoader extends Sprite
{
private var loadedAppDomain:ApplicationDomain;
public function SWFLoader()
{
var request:URLRequest = new URLRequest("test.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onTestLoadComplete);
loader.load(request);
}
private function onTestLoadComplete(event:Event):void
{
var loaderInfo:LoaderInfo = LoaderInfo(event.target);
loadedAppDomain = loaderInfo.applicationDomain;
// Here we can get ANY defined symbol (class, namespace or function according to Adobe Flash help)
var someSymbolClass:Class = Class(loadedAppDomain.getDefinition("SomeSymbol"));
var someSymbolSprite:Sprite = Sprite(new someSymbolClass());
addChild(sprite);
}
}
}
How can I get all of the definitions in a SWF, without specifying each explicitly?
As of Flash Player 11.3, you can use ApplicationDomain.getQualifiedDefinitionNames().
See the official documentation for the method and this blog post about the Flash Player release.
EDIT: This is the quickest solution to your problem : http://www.bytearray.org/?p=175
Hi, you could use this library : https://github.com/claus/as3swf/wiki/
Don't have the time to do deeper test, but here is what i found :
1 - I have created a .swf containing in the library 2 exported MC, $Test and $Test2
2 - Once the .swf loaded by a Loader, i run this code :
var swf : SWF = new SWF(loader.contentLoaderInfo.bytes);
trace(swf);
3 - In the output you'll notice theses lines :
[76:SymbolClass]
Symbols:
[0] TagID: 2, Name: $Test2
[1] TagID: 1, Name: $Test
I think that there is a way to obtain this info directly thru the library API
You have to put the loaded SWF in the current ApplicationDomain.
Use ApplicationDomain.currentDomain to do that, on the ContextLoader info.
loader.load(request, new ContextLoader(false, ApplicationDomain.currentDomain));
It should work.
Following from the answer I received from a previous question I asked here a few days ago (it's about SWC , but in your case, it doesn't really make a difference )
Working with SWCs - getDefinitionByName issue
If both SWFs share the same ApplicationDomain, you should be able to access the loaded SWF classes directly by doing this:
//provided that SomeSymbol extends Sprite...
var someSymbolSprite:Sprite =new SomeSymbol();
On the other hand, you won't be able to do this
var SomeSymbol:Class = getDefinitionByName("SomeSymbol");
unless you create a library of objects from the loaded SWF
var ssym:SomeSymbol;
Check the above link for more details.

How to add all symbols in library folder to stage at runtime

I just submitted this question but i can't see if it posted anywhere, so I apologize if this is a duplicate.
For a Flash CS4 project, I am constantly importing new images, converting them to movieclips and storing them in the library under an "Ornaments" folder. All of these ornaments need to be on the stage in a certain place when the program is initialized. Instead of having to drag the new symbols onto the stage every time I add a new, is it possible to add all of the symbols in the "Ornament" library folder to the stage at runtime?
Thanks
You can do it in code if you wish, but you'd have to still add the names of the symbols to the code. That is, the folder is merely a convenience for organizing within the CS4 library and it does not translate to code (AFAIK).
To instantiate the item in AS3, simply right click the symbol in the library and check the box labelled "Export for ActionScript". If you can't see it, click the Advanced button. It will default the Class to the name of the symbol. That will be the class you can instantiate in ActionScript to put an instance on stage.
You could keep an array of the ornament names and loop through them adding them to the stage:
var ornaments:Array = [OrnamentGold, OrnamentSilver, OrnamentBronze];
for each(var ornament:Class in ornaments)
{
var ornamentClip:MovieClip = new ornament();
addChild(ornamentClip);
}
If you name all of your instances the same with only a trailing digit incremented, you can save yourself some time and just increment a single number:
const NUM_ORNAMENTS:int = 5;
for(var i:int = 0; i < NUM_ORNAMENTS; i++)
{
// ornaments are names Ornament0, Ornament1, Ornament2, etc. in the library
var ornamentClass:Class = new getDefinitionByName("Ornament" + i) as Class;
var ornamentClip:MovieClip = new ornamentClass();
addChild(ornamentClip);
}