external swf files flash - actionscript-3

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*/
}

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

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

Load External SWF through ComboBox AS3

I am trying to create a ComboBox that loads and unloads an external SWF onto stage using ActionScript 3.0 using Flash CS5.
Currently there are 2 list items in the combo box: Home and About.
Upon selecting Home or About option from ComboBox it displays both Home and About SWF at once when selected.
I only want 1 SWF to be displayed only when selected, not all.
menuList.addItem({label:"Choose"});
menuList.addItem({label:"Home",path:"home_load.swf"});
menuList.addItem({label:"About",path:"about.swf"});
menuList.addEventListener(Event.CHANGE, Home);
menuList.addEventListener(Event.CHANGE, About);
var loader:Loader = new Loader();
loader.unloadAndStop();
function Home(e:Event):void
{
if (e.currentTarget.selectedItem.path)
{
var loader:Loader = new Loader();
//loader.unloadAndStop();
loader.load(new URLRequest("home_load.swf"));
addChild(loader);
//loader.unloadAndStop();
loader.x = 0;
loader.y = 190;
}
}
function About(e:Event):void
{
if (e.currentTarget.selectedItem.path)
{
//loader.unloadAndStop();
var loader:Loader = new Loader();
loader.load(new URLRequest("about.swf"));
addChild(loader);
//loader.unloadAndStop();
loader.x = 0;
loader.y = 190;
}
}
You may have to do addChild after the swf file has completed loading.
//These listeners detect when the file has finished loading, and if the
//correct file is loaded.
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
//The load method then loads the SWF file into the loader object.
swfLoader.load(my_url);
//This function adds the external SWF to the stage.
function finishLoading(loadEvent:Event) {
addChild(loadEvent.currentTarget.content);
}
//This function displays a message if the file is not found.
function errorHandler(errorEvent:Event):void {
trace("file missing");
}

Actionscript 3: Converting bytearray to PNG and display on the scene

I'm getting getting a PNG image stored in SQL through a WCF get call. The image is encoded as a base64 string and delivered to my AS3 code. I need to extract the image from the data and show it on the scene.
Among other things, I have also tried this...
var imgArray:ByteArray = Base64.decodeToByteArray(responseXML.ImageObject);
var myRect:Rectangle = new Rectangle(100,100,200,200);
var bmd:BitmapData = new BitmapData(200,200,true,0xAAAAAAAA);
bmd.setPixels(myRect, imgArray);
var image:Bitmap = new Bitmap(bmd,"auto",true);
this.addChild(image);
but to no avail.
HELP!
why don't you use a loader and loadbytes? It's native.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoad)
loader.loadbytes(byteArray);
private function handleLoad(e:Event):void {
var loader:Loader = e.currentTarget as Loader;
// removelistener,etc
trace(loader.content as Bitmap);
}
The problem with your code is that PNG is compressed, bitmap is uncompressed.
I used DanielH's input and got the image to load on my stage. Here is what I did in the event handler function...
function ImageLoaded(e:Event):void
{
var bmd:BitmapData = new BitmapData(imageLoader.ImageLoader.width,imageLoader.ImageLoader.height,true, 0xFFFFFF);
bmd.draw(imageLoader.ImageLoader);
var image:Bitmap = new Bitmap(bmd,"auto",true);
image.width=40;
image.height=40;
if(!CheckAndStoreImageIDKey(imageLoader.ImageID))
{
Images[imageLoader.ImageID] = image;
}
}
Try PNGDecoder (http://www.ionsden.com/content/pngdecoder)
import ion.utils.png.PNGDecoder;
var bmd:BitmapData = PNGDecoder.decodeImage(imgArray);