While I'm not new to Flash builds I ma ccerrtainly new to ActionScript 3.0 so this may be boneheaded issue but any help would be fantastic.
I'm currently trying to use a myLoader variable to load the different pages of my site(.swfs) but for some reason the swfs are just loading on top of the current swf. I've listed the code below. Thanks for any help ya'll can offer
var myLoader:Loader=new Loader ();
ask_btn.addEventListener(MouseEvent.CLICK, askcontent);
function askcontent(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("ask.swf");
myLoader.load(myURL);
addChild(myLoader);
}
inspire_btn.addEventListener(MouseEvent.CLICK, inspirecontent);
function inspirecontent(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("inspire.swf");
myLoader.load(myURL);
addChild(myLoader);
}
discover_btn.addEventListener(MouseEvent.CLICK, discovercontent);
function discovercontent(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("discover.swf");
myLoader.load(myURL);
addChild(myLoader);
}
create_btn.addEventListener(MouseEvent.CLICK, createcontent);
function createcontent(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("create.swf");
myLoader.load(myURL);
addChild(myLoader);
}
nav_blog_btn.addEventListener(MouseEvent.CLICK, goBlog);
function goBlog(e:MouseEvent):void {
navigateToURL(new URLRequest("http://www.3partharmonygroup.com/blog"));
}
nav_ask_btn.addEventListener(MouseEvent.CLICK, navaskcontent);
function navaskcontent(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("ask.swf");
myLoader.load(myURL);
addChild(myLoader);
}
nav_inspire_btn.addEventListener(MouseEvent.CLICK, navinspirecontent);
function navinspirecontent(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("inspire.swf");
myLoader.load(myURL);
addChild(myLoader);
}
nav_discover_btn.addEventListener(MouseEvent.CLICK , navdiscovercontent);
function navdiscovercontent(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("discover.swf");
myLoader.load(myURL);
addChild(myLoader);
}
nav_create_btn.addEventListener(MouseEvent.CLICK, navcreatecontent);
function navcreatecontent(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("create.swf");
myLoader.load(myURL);
addChild(myLoader);
}
nav_contact_btn.addEventListener(MouseEvent.CLICK, navcontactcontent);
function navcontactcontent(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("contact.swf");
myLoader.load(myURL);
addChild(myLoader);
}
The reason why this happens is because whenever you call a function a new loader is added. Take out addChild(myLoader); from all the function and add it only once in the beginning of the script.
var myLoader:Loader=new Loader ();
addChild(myLoader);
ask_btn.addEventListener(MouseEvent.CLICK, askcontent);
I have seen this now quite often. Is that written in some book or tutorial?
Also you can cut some script here as well by once in the beginning declaring
var myURL:URLRequest;
Then later you can just write:
myURL=new URLRequest("contact.swf");
- The right of the People to create Flash movies shall not be infringed. -
Yep, its the Flash CS4 Classroom in Book that tell uses that code to load content. It works great during the tutorial but when I then move to building my own site it gets a bit wonky.
I made the changes you suggested, thank you so much for the direction, but I must be a wee bit more dense than I thought. I'm running intot hte same issue. I posted the movie to my website is that helps.
Thanks again for all the help. It feels as though I'm chasing my own tail for a while now. I think I'm having a fundamental break down in my understanding of Loaders. For what ever reason the mMCs keep loading on top of the base MC.
I tried using the same AS that you used in the Atest file, thank you for doing that, but I still have the same issue. Here's the movie I ended up with. Is there away to make sure that once the buttons are clicked that button and the rest of the buttons "unload" or disappear?
By the way, awesome tutorial on your flashscript.biz site!
Thanks again for all the help. It feels as though I'm chasing my own tail for a while now. I think I'm having a fundamental break down in my understanding of Loaders. For what ever reason the mMCs keep loading on top of the base MC.
I tried using the same AS that you used in the Atest file, thank you for doing that, but I still have the same issue. Here's the movie I ended up with. Is there away to make sure that once the buttons are clicked that button and the rest of the buttons "unload" or disappear?
By the way, awesome tutorial on your flashscript.biz site!
You are using the images as buttons. They are not located in the loader. What i have said is for loading external images. The buttons will not be replaced.
- The right of the People to create Flash movies shall not be infringed. -
var myLoader:Loader=new Loader ();
addChild(myLoader);
Also you can cut some script here as well by once in the beginning declaring
var myURL:URLRequest;
Then later you can just write:
myURL=new URLRequest("contact.swf");
Thank you very much for this simple code.
I searched more than 5 google search pages about "loading external swf" and haven't found a simplier solution than yours.
For the navigation I found a perfect script that I keep reusing it on all of my website projects.
This is the whole code with your simple loader solution.
PHP Code:
//---- add the buttons to an array --------
var buttonsNav:Array = [news_btn,about_btn,reels_btn,work_btn,contact_btn];
//---- add sound for the rollover states of the buttons --------
var snd:soundclick= new soundclick();
var swfLoader:Loader=new Loader();
stage.addChild(swfLoader);
var url:URLRequest;
//----loop thru the buttonsArray-----
//----set some properties and add events to buttons----
function setButtons():void {
for (var i:int=0; i<buttonsNav.length; i++) {
buttonsNav[i].id = i;
buttonsNav[i].buttonMode = true;
buttonsNav[i].mouseChildren = false;
buttonsNav[i].mouseEnabled = true;
buttonsNav[i].addEventListener(MouseEvent.ROLL_OVER,playOver);
buttonsNav[i].addEventListener(MouseEvent.ROLL_OUT,playOut);
buttonsNav[i].addEventListener(MouseEvent.CLICK,doClick);
}
}
//----fires when the mouse rolls over the button----
function playOver(event:MouseEvent):void {
event.currentTarget.gotoAndPlay("navOver");
snd.play();
}
//----fires when the mouse rolls out the button----
function playOut(event:MouseEvent):void {
event.currentTarget.gotoAndPlay("navOut");
}
//----fires when you click on the button
function doClick(event:MouseEvent):void{
//----set the currentBtn variable equal with-----
//----the id of the button that was clicked-----
var currentBtn:int = event.currentTarget.id;
//----call the setSelectedBtn function
setSelectedBtn(currentBtn);
}
/*check to see witch button was clicked
if the id passed to the setSelectedBtn function
is identical with the id of the button clicked
we put that buttons in the down state (selected)
and remove all the events added to it*/
function setSelectedBtn(id:int):void{
for (var i:int=0; i< buttonsNav.length; i++) {
if (id == i) {
buttonsNav[i].gotoAndStop("navDown");
buttonsNav[i].buttonMode = false;
buttonsNav[i].mouseEnabled = false;
buttonsNav[i].removeEventListener(MouseEvent.ROLL_OVER,playOver);
buttonsNav[i].removeEventListener(MouseEvent.ROLL_OUT,playOut);
buttonsNav[i].removeEventListener(MouseEvent.CLICK,doClick);
switch(id){
case 0:
url=new URLRequest("news.swf");
swfLoader.load(url);
break;
case 1:
url=new URLRequest("about.swf");
swfLoader.load(url);
break;
case 2:
url=new URLRequest("showreel.swf");
swfLoader.load(url);
break;
case 3:
url=new URLRequest("work.swf");
swfLoader.load(url);
break;
case 4:
url=new URLRequest("contact.swf");
swfLoader.load(url);
break;
default:
trace("ups, something's wrong");
break;
}
} else {
if(buttonsNav[i].currentLabel =="navDown"){
buttonsNav[i].gotoAndPlay("navOut");
}
buttonsNav[i].buttonMode = true;
buttonsNav[i].mouseEnabled = true;
buttonsNav[i].addEventListener(MouseEvent.ROLL_OVER,playOver);
buttonsNav[i].addEventListener(MouseEvent.ROLL_OUT,playOut);
buttonsNav[i].addEventListener(MouseEvent.CLICK,doClick);
}
}
}
//----call the setButtons function----
setButtons();
//----first button will be selected----
setSelectedBtn(0);
For me, this is the best navigation script I've ever found.
It's easy to customize, cause you only need to change the names of your buttons' instances in the buttonsNav:Array, and create some animated buttons with ROLL_OVER, ROLL_OUT, CLICK states with labels on the buttons' states respectively. (navOver, navDown, navOut)