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

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
}

Related

Flash-Animated-GIF-Library

I'm trying to use the Flash-Animated-GIF-Library. It serves to load an animated gif. It does it with the fileReference class, where you have to browse your folders, choose an animated gif and then it'll show it on the stage. I need the animated gif to show without that browsing part. Is it in anyway possible to use that class to load animated gifs directly, the same way you'd load and show an image with the Loader class? If so - how?
Yes, you have 2 options.
Use the Loader or URLLoader class. Example1, Example 2 (get bytearray)
Embed the image and get ByteArrayAsset. Example
The minimal code for option1 (Loader):
protected function handleCreationComplete(event:FlexEvent):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
loader.load(new URLRequest("yourgif.gif"));
}
private function loaderComplete(event:Event):void
{
    var loaderInfo:LoaderInfo = LoaderInfo(event.target);
    var byteArray:ByteArray = loaderInfo.bytes;     
player.loadBytes(byteArray);
}
The minimal code for option1 (URLLoader):
protected function handleCreationComplete(event:FlexEvent):void
{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loaderComplete);
loader.load(new URLRequest("yourgif.gif"))
}
private function loaderComplete(event:Event):void
{
player.loadBytes(event.target.data);
}
And for option2:
[Embed(source="yourgif.gif",mimeType="application/octet-stream")]
public var YourGif:Class;
protected function handleCreationComplete(event:FlexEvent):void
{
var byteArrayAsset:ByteArrayAsset = new YourGif();
player.loadBytes(byteArrayAsset);
// should work, too
//player.loadBytes(new YourGif() as ByteArray);
}

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...

external swf files flash

I'm trying to link pages together by using swf. I originally used scenes but I heard swf r better so I need help. I have the swf loading but the main menu won't go away. I tried using alpha=0; but it's no good.
code:
vid1_btn.addEventListener(MouseEvent.CLICK, nextPage);
//Create a function for the button click
function nextPage(ev:MouseEvent):void
{
var request:URLRequest = new URLRequest("page1.swf");
var loader:Loader = new Loader()
loader.load(request);
addChild(loader);
//this.background_mc.alpha = 0;
}
You can try
addChildAt(loader, 0);
if your menu is only chlid there.
Or you can set this loader downword by
loader.y = 100;
if your menu is on top of the screen.
First, your code should be somewhat like this:
function nextPage(ev:MouseEvent):void
{
var request:URLRequest = new URLRequest("page1.swf");
var loader:Loader = new Loader()
loader.load(request);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
//this.background_mc.alpha = 0;
}
function swfLoaded(e:Event){
addChild(e.target.content); /* or e.target, i'm not sure*/
}

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.

Actionscript 3, loading image from web

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