Action Script 3 , SharedObject , Save Loader Data - actionscript-3

I have a question
where is my mistake and how can I improve it?
the main thing is Image data have saved but I can't load it again
my data is an image
I get this error every time I have tried to load the loader
TypeError: Error #1034: Type Coercion failed: cannot convert Object#2802e5c9 to flash.display.Loader.
at LoadAndSaveImage_fla::MainTimeline/LOADING_IMAGE()
Here is my code :
var loader:Loader = new Loader();
var MY_DATA:SharedObject = SharedObject.getLocal("Kianoosh");
BROWSE.addEventListener(MouseEvent.CLICK, BROWSE_CLICKED);
SAVE.addEventListener(MouseEvent.CLICK, SAVE_CLICKED);
FR.addEventListener(Event.SELECT, SELECTED);
FR.addEventListener(Event.COMPLETE, COMPLETED);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, LOAD_COMPLETE);
LOADING_BTN.addEventListener(MouseEvent.CLICK, LOADING_IMAGE);
function BROWSE_CLICKED(evt:MouseEvent)
{
FR.browse();
}
function SELECTED(Event)
{
FR.load();
}
function COMPLETED(evt:Event)
{
loader.loadBytes(evt.currentTarget.data);
}
function SAVE_CLICKED(MouseEvent)
{
MY_DATA.data.MY_IMAGE = loader;
MY_DATA.flush();
}
function LOADING_IMAGE(MouseEvent)
{
loader = MY_DATA.data.MY_IMAGE;
}
UIL.addChild(loader);
function LOAD_COMPLETE(Event)
{
loader.width = UIL.width;
loader.height = UIL.height;
}

You can't save any UI object into SharedObject, but you can save loaded bytes.
When you need to recover image, just load bytes and instantiate a new Image.

Related

ActionScript 3.0 import swf

i've little bit trouble with imported swf. Problem is that imported swf has movie clip which exported as movie clip class. Code is here:
Imported swf:
class myBooks(){
var myBg:MovieClip = new backGround();
stage.addChild(myObjects);
and HOLDER flash file:
var txt:String = "Project/book2.swf";
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest(txt);
myLoader.load(url);
addChild(myLoader);
when i try to run , it shows me error like this:
TypeError: Error #1009 cannot access a property or method of a null object reference at actions::myBooks()
Try the following
class myBooks()
{
public function myBooks():void
{
if(stage)
{
init(null);
}
else
{
addEventListener( Event.ADDED_TO_STAGE,init)
}
}
private function init( e:Event = null )
{
var myBg:MovieClip = new backGround();
stage.addChild(myObjects);
}
}
Hope that help

AS3 Load Image Using Path From Text File

i use txt file that contain paths.
after loaded txt file and split to path array
then load img file from array path
but i get err in loading img
please help me
sample code:
var imglst:Array=new Array();
var lodimg:Loader=new Loader();
var lodtxt:URLLoader=new URLLoader();
lodtxt.load(new URLRequest("imglst.txt"));
lodtxt.addEventListener(Event.COMPLETE,onL_C);
function onL_C(e:Event)
{
var t:Array=new Array();
t=e.target.data.split(/\n/);
var s:URLRequest=new URLRequest(t[0].toString());
trace(t[0]);
lodimg.load(s);
}
lodimg.contentLoaderInfo.addEventListener(Event.COMPLETE,onL_Cimg);
function onL_Cimg(e:Event)
{
var i:Bitmap=new Bitmap();
i=Bitmap(lodimg.content);
i.width=100;
i.height=100;
addChild(i);
trace("OK");
}
Are you loading images from a different website from your own? Does the other website(s) have a crossdomain.xml to allow permissions of loading their content? Usually Flash will give you a "security error" as an event but your code isn't listening for such an event so your program is not aware of any such problems... look on google how to handle AS3 errors.
Anyways a workaround is to just load the bytes of file using URLloader and on completion you then only use Loader to decode the bytes into pixel colours.
import flash.utils.ByteArray;
var imglst : Array;
var lodimg : Loader;
//var lodtxt : URLLoader = new URLLoader();
var lodURL:URLLoader = new URLLoader();
var img_bytes : ByteArray = new ByteArray();
lodURL.load(new URLRequest("http://yoursite/imglst.txt"));
lodURL.addEventListener(Event.COMPLETE,onL_C);
function onL_C (e:Event)
{
//# Since load complete no need to keep listening for that
lodURL.removeEventListener(Event.COMPLETE,onL_C);
var t:Array=new Array();
t=e.target.data.split(/\n/);
var s:URLRequest=new URLRequest(t[0].toString());
trace(t[0]);
//lodimg.contentLoaderInfo.addEventListener(Event.COMPLETE,onL_Cimg);
//lodimg.load(s);
//# Now we know path so we load those file bytes
lodURL.dataFormat = URLLoaderDataFormat.BINARY;
lodURL.load(s); lodURL.addEventListener(Event.COMPLETE, onBytes_C);
}
function onBytes_C (e:Event)
{
//# on complete load of bytes...
trace("got bytes");
img_bytes = lodURL.data;
//trace("Image bytes total : " + img_bytes.length);
img_bytes.position = 0; //avoid "end of file" error
lodimg = new Loader();
lodimg.contentLoaderInfo.addEventListener(Event.COMPLETE,onL_Cimg);
lodimg.loadBytes(img_bytes); //we already have data so this will be fast
}
function onL_Cimg (e:Event)
{
var i:Bitmap = new Bitmap();
i = lodimg.content as Bitmap;
i.width=100;
i.height=100;
addChild(i);
trace("OK");
}

How do I make sure a file opened by URLLoader is closed so I can write on it with a FileStrewam

Need to load a file change it and then write the changes back into it. I get a File in use error. How do I make sure the file is closed before I can write on it:
protected function getData():void
{
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(new URLRequest(filePath));
}
protected function onComplete(e:Event):void
{
var data:XML = new XML((e.target as URLLoader).data);
data.replace('User',<User></User>);
var stream:FileStream = new FileStream();
stream.open(new File(filePath),FileMode.WRITE);
stream.writeMultiByte('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<?xml-stylesheet type="text/xsl" href="template.xsl"?>\n'+data.toXMLString(),File.systemCharset);
stream.close();
}
I get the error Error #2044: Unhandled IOErrorEvent:. text=Error #3013: File or directory is in use.
at views::JobsManagement/readyToClean()[C:\Users\David\Adobe Flash Builder 4.7\NxMobileInspect_FixingMemoryIssues\src\views\JobsManagement.mxml:190]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
Already tried opening with method openAsync did not work. Error comes as a error window instead of coming from debugger.
Any ideas?
You can try using urlLoader.close() but personally I use a try catch and then a Timer dealy until the file is available for writing. You can of course limit the number of try catch used. Something like:
protected function onComplete(e:Event):void
{
var data:XML = new XML((e.target as URLLoader).data);
data.replace('User',<User></User>);
saveFile();
}
private function saveFile(e:Event = null):void
{
try
{
//FileStream write
}
catch(er:Error)
{
//set a timer with listener saveFile and try again
}
}

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));

loading external as2 movies

I need to load a swf file into a new swf movie. I need to check last frame to start a movie clip, etc. Everything works ok in the below code. I was using as3 and was loading also an external as3 swf movie. The problem started when I tried to load external as2 swf movies as I receive the message: TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::symbol_name to flash.display.MovieClip. Is there a way I can convert the following code to as2???? Is there another way? Please note I'm an absolute Flash beginner and I've tried my hardest to do this in as3 and now I don't see any alternative but to use as2!!! Many thanks!
var swfLoader:Loader = new Loader();
var swfFile:URLRequest = new URLRequest("file.swf");
var container:MovieClip= new MovieClip();
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadedHandler);
var currentSWF:MovieClip = new MovieClip();
swfLoader.load(swfFile);
container.addChild(swfLoader);
addChild(container);
function swfLoadedHandler(e:Event):void {
currentSWF = MovieClip(swfLoader.content);
currentSWF.addEventListener(Event.ENTER_FRAME, checkLastFrame);
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
currentSWF.stop();
bob.play();
if (bob.currentFrame == 2) {
bob.stop();
}
}
}
}
There is a good answer to the question of loading AS2 content into AS3 here: Load AS2 SWF Into AS3 SWF and pass vars in URL. Basically, you're going to need to create a bridge loader in AS2 if you can't edit the AS2 content you're loading.
Edit: This (untested) code should do what you need it to in AS2:
import mx.utils.Delegate;
var mcLoader:MovieClipLoader = new MovieClipLoader();
mcLoader.onLoadComplete = Delegate.create(this, loadComplete);
var targetMC:MovieClip = createEmptyMovieClip("container",getNextHighestDepth();
mcLoader.loadClip("file.swf",targetMC);
function loadComplete(evt:Object):Void {
targetMC.onEnterFrame = Delegate.create(this,checkFrame);
}
function checkTargetFrame(evt:Object):Void {
if(targetMC._totalframes == targetMC._currentframe) {
targetMC.stop();
targetMC.onEnterFrame = null;
bob.onEnterFrame = Delegate.create(this,checkBobFrame);
bob.play();
}
}
function checkBobFrame(e:Object):Void {
if(bob._currentframe == 2) {
bob.onEnterFrame = null;
bob.stop();
}
}