Skip to main content
January 4, 2010
Question

Button menu to load swf's

  • January 4, 2010
  • 3 replies
  • 686 views

I have been reading tutorials and learning flash.  Forgive my "newbie-ness".

I am trying to make a menu that loads external swf's into my current movie.

Here is the code that I have, which does what I want, except the buttons only work in order... you can't click the last one first, for example...  They only work in the order in which the button variables are scripted... well here....don't laugh.. i am sure there is a better way.

import fl.controls.Button;

var my_loader:Loader = new Loader();
addChild(my_loader);

var about_btn:Button = new Button();
about_btn.label="ABOUT";
about_btn.x=50;
about_btn.y=550;
addChild(about_btn);

var gallery_btn:Button = new Button();
gallery_btn.label="GALLERY";
gallery_btn.x=250;
gallery_btn.y=550;
addChild(gallery_btn);

var contact_btn:Button = new Button();
contact_btn.label="CONTACT";
contact_btn.x=450;
contact_btn.y=550;
addChild(contact_btn);

var partners_btn:Button = new Button();
partners_btn.label="PARTNERS";
partners_btn.x=650;
partners_btn.y=550;
addChild(partners_btn);

about_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void {
my_loader.load(new URLRequest("aboutFlash.swf"));

gallery_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void {
my_loader.load(new URLRequest("galleryFlash.swf"));

contact_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void {
my_loader.load(new URLRequest("contactFlash.swf"));

partners_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void {
my_loader.load(new URLRequest("partnersFlash.swf"));
}

}

}

}

I have been looking at so many snipets and such..  and whats with all the brackets at the end... I kept adding them until it wasn't expecting right bracket at end of program anymore.  Like I said, it works but you have to click "about"  first and so on down the list, once through, then what ever order after you have loaded them all once.

Rick

This topic has been closed for replies.

3 replies

January 4, 2010

Thank you for your help.  That will give something to chew on for a while. I have been looking at tons of tutorial sites and everybody's little snipets of code and that was a mash of a few I think.

Is there a quality site for tutorials and such?  I try not to come here for "training" but my code didn 't register any errors and I was getting confused.  My brain hurts.

R

Inspiring
January 4, 2010

You are welcome.

I never used tutorials - just books, so I am not a good source for this kind of information.

Don't worry, things will fall into place sooner than you think. Just give your brain a little time to digets new information and it will start spitting out wonderful things :-)

Inspiring
January 4, 2010

Oh, and with the way you do it you should probably unload previously loaded content:

function onClick(e:MouseEvent):void {
     trace(e.currentTarget.name);
     my_loader.unload();
     my_loader.load(new URLRequest(e.currentTarget.name + ".swf"));
}

Inspiring
January 4, 2010

You confuse Flash with this code. Only one function with the same name can be in the same scope.

Here is way 1 - keep references to swfs in Dictionary and refer to Dictionary in onClick function:

import fl.controls.Button;
import flash.utils.Dictionary;

var my_loader:Loader = new Loader();
addChild(my_loader);

var about_btn:Button = new Button();
about_btn.label="ABOUT";
about_btn.x=50;
about_btn.y=550;
addChild(about_btn);

var gallery_btn:Button = new Button();
gallery_btn.label="GALLERY";
gallery_btn.x=250;
gallery_btn.y=550;
addChild(gallery_btn);

var contact_btn:Button = new Button();
contact_btn.label="CONTACT";
contact_btn.x=450;
contact_btn.y=550;
addChild(contact_btn);

var partners_btn:Button = new Button();
partners_btn.label="PARTNERS";
partners_btn.x=650;
partners_btn.y=550;
addChild(partners_btn);

// keep references to swf connected
// to buttons
var buttonsSWFs:Dictionary = new Dictionary();
buttonsSWFs[about_btn] = "aboutFlash.swf";
buttonsSWFs[gallery_btn] = "galleryFlash.swf";
buttonsSWFs[contact_btn] = "contactFlash.swf";
buttonsSWFs[partners_btn] = "partnersFlash.swf";

about_btn.addEventListener(MouseEvent.CLICK, onClick);
gallery_btn.addEventListener(MouseEvent.CLICK, onClick);
contact_btn.addEventListener(MouseEvent.CLICK, onClick);
partners_btn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void {
     my_loader.load(new URLRequest(buttonsSWFs[e.currentTarget]));
}

ANother way - keep the references in buttons' names and refer to name in onClick function

import fl.controls.Button;
import flash.utils.Dictionary;

var my_loader:Loader = new Loader();
addChild(my_loader);

var about_btn:Button = new Button();
about_btn.label = "ABOUT";
about_btn.name = "aboutFlash";
about_btn.x=50;
about_btn.y=550;
addChild(about_btn);

var gallery_btn:Button = new Button();
gallery_btn.label = "GALLERY";
gallery_btn.name = "galleryFlash";
gallery_btn.x=250;
gallery_btn.y=550;
addChild(gallery_btn);

var contact_btn:Button = new Button();
contact_btn.label = "CONTACT";
contact_btn.name = "contactFlash";
contact_btn.x=450;
contact_btn.y=550;
addChild(contact_btn);

var partners_btn:Button = new Button();
partners_btn.label = "PARTNERS";
partners_btn.name = "partnersFlash";
partners_btn.x=650;
partners_btn.y=550;
addChild(partners_btn);

about_btn.addEventListener(MouseEvent.CLICK, onClick);
gallery_btn.addEventListener(MouseEvent.CLICK, onClick);
contact_btn.addEventListener(MouseEvent.CLICK, onClick);
partners_btn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void {
     my_loader.load(new URLRequest(e.currentTarget.name + ".swf"));
}

Message was edited by: Andrei1