AS3: use buttons in external swf - actionscript-3

I am making a website in Adobe Flash that imports it´s navigation buttons from an external swf-file.
The problem is how my Main FLA-file will know which of the navigation buttons the users has pressed, since the buttons and it's eventListeners are in the external swf-file.
With other word: can I make my external swf-file return a Number to my website FLA-file, to determine which button that have been pressed? If so, how?

You can set up a connection between the external swf and main swf using the LocalConnection()
In main.swf
var sending_lc:LocalConnection;
sending_lc = new LocalConnection();
function send_it(evt:MouseEvent):void
{
sending_lc.send("connectionName", "functionName", "myData");
//params are (connection name, function to execute in the receiving swf, the data to pass through)
}
my_btn.addEventListener(MouseEvent.MOUSE_UP, send_it);
In exetrnal.swf
var receiving_lc:LocalConnection;
receiving_lc = new LocalConnection();
receiving_lc.connect("connectionName");
receiving_lc.client = this;
function functionName(data:String):void {
trace(data);
}

Related

AS3 Loading multiple external SWFs and them closing them

I am trying to load multiple external SWFs in one main SWF.
I start with the main swf. Now I need to link 4 games to it.
I have created 4 different loaders and I make it load each game into a different frame for each game. Ie when I go to frame 1 for example it will load game1. The code I am using for each game frame is:
public function LoadGame1(evt:MouseEvent):void {
trace("load game1");
this.gotoAndPlay("Game1");
var url:URLRequest = new URLRequest("game1.swf");
game1Loader.load(url);
addChild(game1Loader);
}
game 2 is the same with the exception of game1 it will be game2 etc.
Now my issue is when I close each game and go to another it does not work. I can not load another game or reload it. When I go back to the menu I use:
public function backFunc(evt:MouseEvent):void {
trace("menu");
myLoader.unload();
game1Loader.unload();
game2Loader.unload();
game3Loader.unload();
game4Loader.unload();
this.gotoAndPlay("Menu");
}
I'm assuming it has something to do with the unloading - it unloads it all and closes the loader. Ive tried removeChild(game1Loader) etc but it doesn't work? The game will close and it will go back to the menu but then I will not be able to get back into the game or load another one?
Please help :(
Flash timelines are kind of like a static state machine; moving from frame-to-frame will run all of the document code at that frame (every time). It also resets the value of the content to the state it was in during design time (so, frame = design + code). Because of the headaches this model can cause, I highly recommend you do all of your design & code in a single frame.
The way you've written your sample code, it seems to imply that you're writing custom functions for each of your game loaders. You can unify that by driving all the btn events through the same function and depending on some differentiating property (like their names), load the appropriate game.
Finally, I suspect the reason your loaders stop working is because you've unloaded and removed them from the stage. That stuff gets garbage collected. Rather than making the loader a global object, feel free to create a new one each time you make your loadGame() call.
// We'll store our buttons in an array to make it easier to register for events.
var btns:Array = [
btn_Load1,
btn_Load2,
btn_Load3
]
// Now cycle over them in bind to our listener
for each (var btn:MovieClip in btns) {
btn.addEventListener("mouseUp", btnEvents);
}
function btnEvents(e:MouseEvent):void {
// With one event listener, we can sort out each condition
switch (e.currentTarget.name) {
case "btn_Load1":
loadGame("game1.swf")
break;
case "btn_Load2":
loadGame("game2.swf")
break;
case "btn_Load3":
loadGame("game3.swf")
break;
}
}
function loadGame(url:String):void {
// Now we only have to write our loader code once (for all buttons)
trace("Loading " + url);
// Because we're unloading our swfs (and unloaded assets get garbage collected),
// we'll want to make a new loader each time we call a load op
var loader:Loader = new Loader();
loader.name = "current_game";
loader.load(new URLRequest(url));
addChild(loader);
}
function backFunc(evt:MouseEvent):void {
// Dynamically find our current game, and unload it.
trace("menu");
getChildByName("current_game").unload();
}

AS3 ahow to control the pause button of the external swf

I just trying to control buttons of external loaded swf .
var swfRequest:URLRequest = new URLRequest('http://mysite.com/player/001.swf');
var swfLoader:Loader = new Loader();
swfLoader.load(swfRequest);
var holder:MovieClip = new MovieClip();
holder.addChild(swfLoader);
addChild(holder);
And in main swf I created new button with Instance Name : pauseBtn , I want this button communicate to external swf witch has pause button with Instance Name : pausebtn in action script 2.0
external btn:
on (release)
{
status_playing = false;
playbtn._visible = true;
pausebtn._visible = false;
stop ();
}
Please help me how to communicate with these buttons.
Normally, I don't think you can directly communicate between AS3 content and AS2 content. One way around it is using a LocalConnection object to handle the communication between the two.
How it would work after creating the objects and connecting them, is the main swf would send a message to it's LocalConnection object and the loaded swf would receive that message on and use it to handle whatever you need it to.
LocalConnection - AS3 Reference: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/LocalConnection.html
LocalConnection - AS2 Reference: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001176.html#312868

complete unload external swf and sound

I am using this code for load and unload an external swf for a button. when I click on button, swf loads and when again I click on button swf unloads, but sound still exist and just swf screen disappeares.
Please correct this code, I want swf to completetely unload.
printer.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);
var fl_Loader:Loader;
//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad:Boolean = true;
function fl_ClickToLoadUnloadSWF(event:MouseEvent):void
{
if(fl_ToLoad)
{
fl_Loader = new Loader();
addChild(fl_Loader);
fl_Loader.load(new URLRequest("quiz.swf"));
printer.x=100;
printer.y=100;
}
else
{ fl_Loader.unloadAndStop();
removeChild(fl_Loader);
fl_Loader = null;
printer.x=155;
printer.y=334;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad = !fl_ToLoad;
}
If you can access the code in loaded swf than you may add Event.UNLOAD listener. In the event handler you can do some cleaning:
stop all sounds,
stop videos,
remove event listeners (specially these attached to the Stage)
This way, the unloading should be smooth and easy.

Unable to load SWF in as3

I am working in flash and as3. I am new to as3. I was trying to load and unload the SWF file.
My project contains 2 files, one is index file and the other is animal file. In the index page I have button for animal. When I click on this button the animal SWF starts executing. But the problem is when I click on index button of animal SWF, it is not showing index page again, instead it is showing message as
Unable to load SWF
My index page code is:
var urlReq:URLRequest = new URLRequest("animal/animal.swf");
swfLoader.load(urlReq);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadComplete);
swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,
swfLoadError);
function swfLoadComplete(evt:Event):void
{
var loader:Loader = Loader(evt.target.loader);
addChild(loader.content);
swfLoader.removeEventListener(Event.COMPLETE, swfLoadComplete);
}
function swfLoadError(evt:IOErrorEvent):void
{
trace("Unable to load swf ");
swfLoader.removeEventListener(IOErrorEvent.IO_ERROR, swfLoadError);
}
So what to do to load the index SWF from animal SWF?
You probably forgot to add to the displaylist. Use addChild.
var urlReq:URLRequest = new URLRequest("animal/animal.swf");
swfLoader.load(urlReq);
this.addChild(swfLoader);
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html
Update: I see its not loaded. You have an IOErrorEvent.IO_ERROR. Mostly this means the file is not located at that url. Check the location or use a http debugger to find out which location you are trying to open.

Making a button that loads into a loaded swf file and unloads the swf on click

I am doing a portfolio type project and am having trouble creating a button that appears in my loaded swf files. I am very new to flash. The portfolio has 2 projects that get loaded when they are clicked. I need to also have a button that appears when they are click as well.
Right now I have it set to unload on click again but that doesn't work because one of the projects is interactive. My code so far is
var myLoader:Loader=new Loader ();
project2.addEventListener(MouseEvent.CLICK, project2content);
function project2content(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("Project2.swf");
myLoader.load(myURL);
addChild(myLoader);
}
project3.addEventListener(MouseEvent.CLICK, project3content);
function project3content(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("Project3.swf");
myLoader.load(myURL);
addChild(myLoader);
}
myLoader.addEventListener(MouseEvent.CLICK, unloadcontent);
function unloadcontent(myevent:MouseEvent):void {
removeChild(myLoader);
project2.gotoAndPlay(1);
project3.gotoAndPlay(1);
}
If I understand you correctly, you want to be able to add a close button to one or both of your project SWFs and have your parent movie pick up clicks on those buttons.
If so, try creating a button in your project SWFs and including the following code:
closeButton.addEventListener(MouseEvent.CLICK, closeHandler);
function closeHandler(event:MouseEvent):void
{
dispatchEvent(new Event("close"));
}
And replace the code which is related to the click event on myLoader with the following:
myLoader.content.addEventListener("close", closeHandler);
function closeHandler(event:MouseEvent):void {
removeChild(myLoader);
project2.gotoAndPlay(1);
project3.gotoAndPlay(1);
}