Unloading an swf file and loading another swf file in as3 - actionscript-3

I want to unload a previously loaded swf file(welcome.swf) and load another swf file(topics.swf) when a user clicks a button that is located inside the first loaded swf file(i.e the button, is nested inside the welcome swf file that is to be unloaded).I have tried all manner of codes but none seems to be working and Im at my wits end.Kindly someone assist.Thanks in advance.Below is the code in my main file and the external swf that is to be unloaded.
Main file
var box:Sprite = new Sprite;
box.x = 0;
box.y = 0;
addChild(box);
var loader = new Loader();
var swfURLReq:URLRequest = new URLRequest("welcome.swf");
loader.load(swfURLReq);
box.addChild(loader);
External swf file
var loader = new Loader();`
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
loader.unloadAndStop();
loader=null;
var SWFRequest:URLRequest = new URLRequest("Topics.swf");
loader.load(SWFRequest);
loader.x = 0;
loader.y = 0;
addChild(loader);
}

I think you should load topics.swf at Main Loader.
So you should access Main Loader from welcome.swf.
Doesn't this code work?
External swf file
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
var target: Object = this;
// Search the Main Loader from child swf.
while (target.parent){
target = target.parent;
if (target is Loader){
var loader:Loader = target as Loader;
var SWFRequest:URLRequest = new URLRequest("Topics.swf");
loader.load(SWFRequest);
break;
}
}
}

I would probably move all the loading/unloading responsibility in a main file, with inner swf just letting the main know about some events inside through loaderInfo.sharedEvents just like this:
main file:
var box:Sprite = new Sprite;
box.x = 0;
box.y = 0;
addChild(box);
var loader = new Loader();
var swfURLReq:URLRequest = new URLRequest("welcome.swf");
loader.load(swfURLReq);
function topicsRequestedHandler(event:Event):void {
loader.contentLoaderInfo.sharedEvents.removeEventListener(
"loadTopics", topicsRequestedHandler);
loader.unloadAndStop();
//load "Topics.swf" here
}
loader.contentLoaderInfo.sharedEvents.addEventListener(
"loadTopics", topicsRequestedHandler);
box.addChild(loader);
and in external file:
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
loaderInfo.sharedEvents.dispatchEvent(new Event("loadTopics"));
}
(not tested but should work)
So using this scheme for all the loaded files, you will keep loading/unloading management in one place.

Related

I am looking for a simple solution to loading a new/different mp3 sound file upon clicking a thumbnail using my existing code below

This seems simple enough, I just can't crack it though.
currently, in the below code. its a UILoader with a series of thumbnail mc's horizontally across the bottom of the FLA. when the thumbs are clicked and new SWF loads in the UI.
I am simply looking for the error of my ways, hopefully by simply added to this code and a possible explanation. I need to figure out how to play/stop on exit and new mp3 sound file.
UILoader
With movieClips used for thumbnail loads
var imagesXML:XML;
var xmlLoader: URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("lessons/images/Images5.xml"));
/////////////////////////////////////////////////////////////
//////////// thumbnail loader /////////////////////////
function xmlLoaded(evt:Event):void
{
imagesXML = new XML(xmlloader.data);
var thumbLoader:UILoader;
for(var i:uint = 0; i < imagesXML.image.length(); i++)
{
thumbLoader UILoader(getChildByName("thumb" + 1));
thumbLoader.load(new URLRequest("lessons/images/thumbs/" + imagesXML.image[i].#file));
thumbLoader.buttonmode = true;
thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
var fullPath:String = "lessons/images/file-1.swf";
mainLoader.load(new URLRequest(fullpath));
}
}
/////////////////////////////////////////////////////////////
//////////////// load Images /////////////////////////////
function thumbClicked(evt:MouseEvent):void
{
var thumbName:String = evt.currentTarget.name;
var thumbIndex:uint = uint(thumbName.substr(5));
var fullPath:String = "lessons/images/" + imagesXML. image[thumbIndex].#file;
mainLoader.load(new URLRequest(fullPath));
}
you can try to use this library https://github.com/treefortress/SoundAS
or try to google "Sound manager for as3" and I sure you will find library you like
Declare sound object outside of functions so it is available to all functions
var mySound:Sound;
var myChannel:SoundChannel;
function thumbClicked(evt:MouseEvent):void
{
var thumbName:String = evt.currentTarget.name;
var thumbIndex:uint = uint(thumbName.substr(5));
var fullPath:String = "lessons/images/" + imagesXML. image[thumbIndex].#file;
mainLoader.load(new URLRequest(fullPath));
//stop any already playing sound
if ( myChannel != null) { myChannel.stop(); }
//load an MP3 file
mySound = new Sound(); mychannel = new SoundChannel();
mySound.load(new URLRequest("myAudio.mp3"));
mySound.play();
}

How to load image to swf which is loaded into other swf? (AIR, AS3)

I have an application in AIR, which is loading swf files and makes previews of them. When those swfs loads other content (eg. jpg) then i have an error, that url not found - i am convinced that is about jpg file. This problem shows only when i load swf by filereference using loadBytes(). When i load swf by Loader using load() - problem doesn`t exist. How can i fix this issue? Sorry for my english. I attach code fragment with loading swfs.
private static function loadFile(loadType:String, swfNum:Number = 0):void {
lc = new LoaderContext();
lc.allowCodeImport = true;
lc.allowLoadBytesCodeExecution = true;
currentSwfNum = swfNum;
var fRef:FileReference = DataBase.swfsList[currentSwfNum];
fRef.addEventListener(Event.COMPLETE, onFileReferenceLoad);
fRef.load();
}
private static function onFileReferenceLoad(e:Event):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ifComplete);
loader.loadBytes(e.target.data, lc);
}
private static function ifComplete(e:Event):void {
var mc:MovieClip = LoaderInfo(e.target).content as MovieClip;
mc.addEventListener("SHOOT", SwfSingleCapture.captureSingleShoot);
}
DataBase.swfList - it is an array of FileReference objects, which are selected by FileReferenceList and browse().

as3 loaded swf accessing variables

I have some questions with sharing/using/accessing variables/functions between loaded swf files.
my prj consists of main.swf file and 2 swf's which I load on first init of the main.swf.
my questions are:
1.how can I use variables from 1.swf in 2.swf (function is running in 2.swf)
2.how can I call a function from 2.swf in 1.swf
here is the code I'm using to load the swf's:
var playerMc:MovieClip = new MovieClip();
var dbMc:MovieClip = new MovieClip();
var m2Loader:Loader = new Loader();
var mLoader:Loader = new Loader();
startLoad();
function startLoad()
{
//var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("./_player/player.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc);
mLoader.load(mRequest);
addChild(mLoader);
//var m2Loader:Loader = new Loader();
var m2Request = new URLRequest("./_db/db.swf");
m2Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc2);
m2Loader.load(m2Request);
addChild(m2Loader);
}
function loadMc(event:Event):void
{
if (! event.target)
{
return;
}
playerMc = event.target.content as MovieClip;
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc);
}
function loadMc2(event:Event):void
{
if (! event.target)
{
return;
}
dbMc = event.target.content as MovieClip;
dbMc.x = -400;
m2Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc2);
}
You have to stick with application domain.
In most cases you should load another swf in another application domain, but it's not really related to your question.
From loader, you must access to applicationDomain and then getDefinition. From there, you can get classes and use them in your main swf. Yes, you can read static properties.
If you need instances you should access loader#content. It is pointing to a root of loaded SWF. Root of loaded is SWF – is the instance of main class of the loaded swf.
Create a variable with no definition such as
public var MyClass;
as you can see i didnt add
public var MyClass:Class;
then in another function write
this.MyClass = this.mLoader.contentLoaderInfo.applicationDomain.getDefinition("NameOfClass") as Class;
i dont know much about this myself.. im having problems figuring out if you can only access Public static variables or if its possible to access normal public variables and possibly private variables because it is creating a new instance of the same class or however you want to word it..?
also after your write the above code .. when you want to change a varaibles this usually works for me
this.MyClass.RandomVariableName = this.MyClass.RandomVariableName + 1;
something like that..

AS3 externally loaded swf to control stage and MovieClips and their children?

I have 2 swf files. One is loaded and loading an externally loaded swf into it.
Here is the code in the first loaded swf: #1
var logo:Loader = new Loader();
logo.load(new URLRequest("images/Logo.png"));
logo.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadLogo);
function LoadLogo(e:Event):void
{
addChild(logo);
}
/// The SWF I AM LOADING
var Beau:Loader = new Loader();
Beau.load(new URLRequest("Beau.swf"));
Beau.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadBeau);
function LoadBeau(e:Event)
{
addChild(Beau);
}
NOW HERE IS THE CODE FOR THE BEAU SWF LOADED: #2
import flash.display.MovieClip;
bird.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
//// I WANT TO BE ABLE TO ADD CODE TO CONTROL MOVIECLIPS
//// AND CHILDREN form this externally loaded swf. How do
//// I do that? Below code does not work.
var logo:MovieClip;
var ControlLogo:MovieClip = MovieClip(logo.content);
ControlLogo.alpha = .3;
}
EDIT
ok your code and tutorials are great. I am having major SANDBOX issues now when I make the clip load from an external URL. I am also coding for an ANDROID using FLASH CS5.5. Its not allowing me to use Security.allowDomain("*");
BOTTOME LINE IS THE NEWLY LOADED SWF can not access the PARENT WITHOUT PERMISSION BUT I DON'T KNOW HOW TO GIVE IT PERMISSION.... using the AIR/ANDROID PLAYER.
use property parent
parent gives loader
parent.parent gives swf#1
So you can write function as given below
function fl_MouseClickHandler(event:MouseEvent):void
{
var swf1:Object = parent.parent;
var logo:Loader = swf1.logo;
var ControlLogo:Bitmap = Bitmap(logo.content); // because logo loads an image
ControlLogo.alpha = .3;
}
make sure that logo is not a local variable, but global and public in swf#1
public var logo:Loader = new Loader();
EDIT:
logo:Loader gets added to SWF#1. Loads image, so content is Bitmap.
beau:Loader gets added to stage. Loads swf (SWF#2), so content is MovieClip.
so now
root is SWF#1
parent of beau is SWF#1
parent of SWF#2 is beau and parent of beau is SWF#1
so for SWF#2, SWF#1 is parent of parent so parent.parent
If you are not creating public variables, you can manage this by using property name.
var logo:Loader = new Loader();
logo.name = "logo";
...
...
var beau:Loader = new Loader();
beau.name = "beau";
...
...
Then anywhere in swf#2
var swf1:Object = parent.parent;
var logo:Loader = Loader(swf1.getChildByName("logo"));
....
....
For accessing the content it is recommended to use type casting as I have shown
var ControlLogo:Bitmap = Bitmap(logo.content); // because logo loads an image
so that you can check as shown below and avoid runtime errors
var ControlLogo:Bitmap = Bitmap(logo.content);
if(ControlLogo){
}
If you want to so something irrespective of content do as shown below
var ControlLogo:Object = logo.content;
if(ControlLogo && ControlLogo.hasOwnProperty("alpha")){
ControlLogo.alpha = 0.4;
}

ActionScript 3.0: load an image programmatically

I'm developing an Blackberry Playbock app with ActionScript 3.0. I'm very very new with this.
I have the following project structure (I'm using Adobe Flash Builder "Burrito"):
project
|
src
|
assets
|
images
On image folder I have several PNGs images that I want to load programmatically.
How can I do that?
And
What GUI component I must use to show an image?
This example loads one image and uses buttons to change it:
// create a Loader object to hold things that you will load
var myLoader:Loader = new Loader();
// position the Loader
myLoader.x = 250;
myLoader.y = 0;
// put something into the Loader
myLoader.load(new URLRequest("tree.jpg"));
// make the Loader visible
addChild(myLoader);
// button listeners
top_btn.addEventListener(MouseEvent.CLICK, loadPhoto);
last_btn.addEventListener(MouseEvent.CLICK, unloadAny);
// button functions
function loadPhoto(e:MouseEvent):void {
myLoader.load(new URLRequest("sailboat.jpg"));
addChild(myLoader);
}
// this function empties the Loader object
function unloadAny(e:MouseEvent):void {
removeChild(myLoader);
}
Use the Loader class.
if u want to show multiple image using for loop then follow this code:
var IMAGE_URL:String = new String("image/");
for(var i=1;i<=13;i++){
addChild(imageLoder(i,i*25));
}
private function imageLoder(ranvalue:int,cor:int):Loader{
var ldr:Loader = new Loader();
ldr.load(new URLRequest(IMAGE_URL+ranvalue+".jpg"));
var xcord=5;
var ycord=5;
ldr.x = cor;
ldr.y = ycord+20;
return ldr;
}