Actionscript 3, loading image from web - actionscript-3

I'm trying to load an external image into my air app.
CODE HERE (http://pastie.org/2314164)
xml.artist_pic is pointing to image example: http://www.example.com/image.jpg
GLOBAL.skin.albumArt is a movie clip which inside has bitmap.
What am I doing wrong?

You need to use the Loader class instead of URLLoader
var loader:Loader = new Loader();
loader.load(new URLRequest(url));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
function imageLoaded(e:Event):void {
var image:Bitmap = (Bitmap)(e.target.content);
}

Related

Flash AS3 load and replace movie

I have a loader mc...an animation that loops - when you click a button I want a new movie to load on top and replace the loader mc
Here is my current code:
btn_load.addEventListener(MouseEvent.CLICK, btn_load_press);
function btn_load_press(e:MouseEvent)
{
var reloadRequest:URLRequest = new URLRequest("new-movie.swf");
var loader:Loader = new Loader();
loader.load(reloadRequest);
addChild(loader);
}
But this puts it on top on my loader...effectively meaning the loader is still running in the background.
This is really simple in AS2 - and I'm not sure if it's even possible in AS3
To do this we'll need to add a listener for your loader.contentLoaderInfos complete event, then remove your animation once everything has finished loading.
btn_load.addEventListener(MouseEvent.CLICK, btn_load_press);
function btn_load_press(e:MouseEvent)
{
var reloadRequest:URLRequest = new URLRequest("new-movie.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loader_complete);
loader.load(reloadRequest);
addChild(loader);
}
function loader_complete(e:Event)
{
e.target.removeEventListener(Event.COMPLETE,loader_complete);
removeChild(loader_mc);
}

How to load new swf on mouseclick event of movieclip in as3?

I want to create a flip effect button, on click event of which a external swf should be opened removing all the contents of current swf.
I have created both components, a button and also a movieclip.
Neither of its load ( new URLRequest()) code working.
Below is the complete code I am using. Where btn2 is class name of my movie clip
and 2.swf is external swf I want to load.
stop();
var bc2:btn2 = new btn2();
bc2.buttonMode = true;
bc1.addEventListener(MouseEvent.CLICK, mouseClick);
var loader = new Loader();
function mouseClick(event:MouseEvent): void {
loader.unload();
loader.load(new URLRequest("2.swf"));
addChild(loader);
}
First btn2 instance what you are instantiating needs to be added for it to be visible.
Secondly you are trying to load the swf even before its loaded.
I have modified the code
stop();
var bc2:btn2 = new btn2();
bc2.buttonMode = true;
this.addChild(bc2);
bc2.addEventListener(MouseEvent.CLICK, mouseClick);
var loader = new Loader();
function mouseClick(event:MouseEvent): void {
loader.unload();
loader.load(new URLRequest("2.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
}
function completeHandler(event:Event):void{
this.addChild(loader);
}
This should work...

How to create a sprite from an image loaded in Loader()?

I'm loading a background image via the Loader() class and wanted to know if there's a way to create a sprite from that loaded image?
I'm wanting to put a function in an external class file to put the image in the loader and then call the class to create a sprite from the loaded image. I'm not even sure this is possible.
Note: I'm using flashdevelop and no timeline.
You can just use the loader object as a display object or you can access the Bitmap object in the loader and add that to a sprite.
var loader:Loader = new Loader();
loader.load(new URLRequest(filename));
addChild(loader);
loader.x = 100;
loader.y = 200;
//so on
To get access to the bitmap and bitmapdata loaded just add an event listener and access them.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.load(new URLRequest(filename));
private function onLoadComplete(e:Event):void
{
var loaderInfo:LoaderInfo = e.target as LoaderInfo;
var loadedBitmap:Bitmap = loaderInfo.content as Bitmap;
var sprite:Sprite = new Sprite();
sprite.addChild(loadedBitmap);
addChild(sprite);
sprite.x = 100;
sprite.y = 200;
//so on
}

Instantiate Document Class in as3

hi need to instantiate document class in as3. I have a main movie which will load load.swf. The document class of load.swf is LoadedMovie. i use this code :
public function Main()
{
var url:URLRequest = new URLRequest("load.swf");
loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
loader.load(url);
}
function finishLoading(e:Event)
{
var sn:LoadedMovie=new LoadedMovie();
addChild(sn);
}
it's added the movieclip and but my framescripts were not working. i have stop at last frame.
Is it any other way to do this.
The Loader will contain an instance of your loaded swf's document class once it's loaded. Simply addChild the loader and you're in business.
var url:URLRequest = new URLRequest("load.swf");
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
loader.load(url);
addChild(loader);
Instanciating from a loaded swf is slightly more complicated, you have to deal with application domains and such.

Loading images and adding to Stage in Actionscript 3

I want to load an image object and add it to the stage at runtime. How would I accomplish this?
if it's an external image you would use this:
var loader:Loader = new Loader();
loader.load(new URLRequest("http://urltoyourimage.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
function onCompleteHandler(event:Event):void
{
event.target.removeEventListener(Event.COMPLETE, onCompleteHandler);
addChild(event.target.content);
}
You use a Loader.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Loader.html
There's a code example near the bottom.