Link to home
Start Free TrialLog in
Avatar of neachtain
neachtain

asked on

Target specific frame in child swf

I need to load several external swf's into a main swf beased on whatever button a user selects on the main swf. This is working just fine.

Some external swfs have subsections, like chapters in a book, that are frame labels. I want to create a "submenu" on the main file that loads the external swf, but goes to a specific frame on that swf. I just can't get it working.

The code with my main button function and loader is below.
stop();
 
var Xpos:Number=10.9;
var Ypos:Number=120;
var swf:MovieClip;
var loader:Loader=new Loader();
 
var defaultSWF:URLRequest = new URLRequest("swfs/introduction.swf");
 
loader.load(defaultSWF);
loader.x=Xpos;
loader.y=Ypos;
addChild(loader); 
function btnClick(event:MouseEvent):void {
removeChild(loader);
var newSWFRequest:URLRequest=new URLRequest("swfs/" + event.target.name + ".swf"); 
//load the loader:
loader.load(newSWFRequest);
loader.x=Xpos;
loader.y=Ypos;
addChild(loader);
}
 
//Main Btn listeners:
letter.addEventListener(MouseEvent.CLICK, btnClick);
who.addEventListener(MouseEvent.CLICK, btnClick);
what.addEventListener(MouseEvent.CLICK, btnClick);
audiences.addEventListener(MouseEvent.CLICK, btnClick);
community.addEventListener(MouseEvent.CLICK, btnClick);
//financials.addEventListener(MouseEvent.CLICK, btnClick);

Open in new window

Avatar of rascalpants
rascalpants
Flag of United States of America image


I don't see any place in your code that does something like...  gotoAndPlay("FrameLabel");

it looks like you need to put an event listener on your Loader, and then tell that loader to go to the frame label you want...

now, I was not able to test my code, cause I don't have a prototype setup for your project, so let me know if you get any errors, and we can resolve them...


rp / ZA

stop();
 
var Xpos:Number=10.9;
var Ypos:Number=120;
var swf:MovieClip;
 
var labelArray:Array = new Array();
labelArray["introduction"] = "frame1";
labelArray["letter"] = "frame7";
labelArray["who"] = "whoFrame9";
labelArray["what"] = "frame1";
labelArray["audiences"] = "start";
labelArray["community"] = "frame8";
 
 
var theLoader:Loader=new Loader();
 
var defaultSWF:URLRequest = new URLRequest("swfs/introduction.swf");
 
theLoader.load(defaultSWF);
theLoader.addEventListener( Event.INIT, on_Init );
 
function btnClick(event:MouseEvent):void
{
	removeChild(theLoader);
	var newSWFRequest:URLRequest=new URLRequest("swfs/" + event.target.name + ".swf"); 
	//load the loader:
	
	//might need to create a new loader, and assign the event listener here to on_Init	
	
	theLoader.load(newSWFRequest);
	
}
 
 
function on_Init( evt:Event ):void
{
	var theLabel:String = labelArray[ evt.target.name ];
	swfLoaded( theLabel );
}
 
function swfLoaded( theLabel:String = null ):void
{
	theLoader.x = Xpos;
	theLoader.y = Ypos;
	addChild(theLoader);
	theLoader.gotoAndPlay( theLabel );
}
 
//Main Btn listeners:
letter.addEventListener(MouseEvent.CLICK, btnClick);
who.addEventListener(MouseEvent.CLICK, btnClick);
what.addEventListener(MouseEvent.CLICK, btnClick);
audiences.addEventListener(MouseEvent.CLICK, btnClick);
community.addEventListener(MouseEvent.CLICK, btnClick);
//financials.addEventListener(MouseEvent.CLICK, btnClick);
 
		

Open in new window

Avatar of neachtain
neachtain

ASKER

OK, Rascalpants. Thanks.
The reason there's no ..gotoAndPlay.. is that I couldn't get that to work.
Before I try this, I have a question, please.
-Since I have sort of a universal button function that targets the name of the swf to be loaded based on the instance name of my button, does that mean that essentially all I have to do is load every button and where it needs to go into the array that you set up?
Thank you!

the array I setup is an associative array, so I just used the instance name of the button as the parameter that can be used to access the frame label...

so the button letter uses...   labelArray["letter"] = "frame7";  which would go to and play a frame with the label of "frame7"

so basically, you can use the instance name in the array...  


rp / ZA


OK. So the error I got is below:

////////////////
1061: Call to a possibly undefined method gotoAndPlay through a reference with static type flash.display:Loader.

theLoader.gotoAndPlay(theLabel)
////////////////////
Any suggestions for correcting this?

Thanks!
stop();
 
var Xpos:Number=10.9;
var Ypos:Number=120;
var swf:MovieClip;
var loader:Loader=new Loader();
 
var labelArray:Array = new Array();
//labelArray["introduction"] = "1";
//labelArray["letter"] = "frame7";
//labelArray["who"] = "whoFrame9";
//labelArray["what"] = "frame1";
//labelArray["audiences"] = "start";
//labelArray["community"] = "frame8";
labelArray["communityHospitals"] = "5";
labelArray["communitySupport"] = "support";
 
 
 
var theLoader:Loader=new Loader();
 
var defaultSWF:URLRequest = new URLRequest("swfs/introduction.swf");
 
theLoader.load(defaultSWF);
theLoader.addEventListener( Event.INIT, on_Init );
 
function btnClick(event:MouseEvent):void
{
	removeChild(theLoader);
	var newSWFRequest:URLRequest=new URLRequest("swfs/" + event.target.name + ".swf"); 
	//load the loader:
	
	//might need to create a new loader, and assign the event listener here to on_Init	
	
	theLoader.load(newSWFRequest);
	
}
 
 
function on_Init( evt:Event ):void
{
	var theLabel:String = labelArray[ evt.target.name ];
	swfLoaded(theLabel);
}
 
function swfLoaded(theLabel:String = null):void
{
	theLoader.x = Xpos;
	theLoader.y = Ypos;
	addChild(theLoader);
	theLoader.gotoAndPlay(theLabel);
}
 
//Main Btn listeners:
letter.addEventListener(MouseEvent.CLICK, btnClick);
who.addEventListener(MouseEvent.CLICK, btnClick);
what.addEventListener(MouseEvent.CLICK, btnClick);
audiences.addEventListener(MouseEvent.CLICK, btnClick);
community.addEventListener(MouseEvent.CLICK, btnClick);
//financials.addEventListener(MouseEvent.CLICK, btnClick);
communityHospitals.addEventListener(MouseEvent.CLICK, btnClick);
communitySupport.addEventListener(MouseEvent.CLICK, btnClick);

Open in new window


try changing...

theLoader.gotoAndPlay(theLabel);

to this...

var clip:MovieClip = theLoader as MovieClip;
clip.gotoAndPlay(theLabel);


rp / ZA

I replaced the code and it makes sense, but I am getting another output error:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
      at flash.display::DisplayObjectContainer/removeChild()
      at ar6_invisibleLoaderArray_fla::MainTimeline/btnClick()

This happens with every button, and the default swf doesn't load. And again, I am too inexperienced to work out where I've gone wrong.
Thanks for any help!

remove this line of code:

removeChild(theLoader);

I was thinking earlier that by removing the loader that we would have problems if we did not create a new one...  but I don't believe there is any need to remove it


rp / ZA
OK - I tried removing removeChild(theLoader), but that didn't work. I moved a couple of things to the btnClick function, which heloed - now the swfs load on button click. However, the defaultSWF doesn't load on startup, and the swf's still don't load at the right frame. They all just load from the beginning.

Getting closer!
stop();
var Xpos:Number=10.9;
var Ypos:Number=120;
var swf:MovieClip;
var loader:Loader=new Loader();
 
var labelArray:Array = new Array();
//labelArray["introduction"] = "1";
//labelArray["letter"] = "frame7";
//labelArray["who"] = "whoFrame9";
//labelArray["what"] = "frame1";
//labelArray["audiences"] = "start";
//labelArray["community"] = "frame8";
labelArray["communityHospitals"] = "hospitals";
labelArray["communitySupport"] = "support";
 
 
 
var theLoader:Loader=new Loader();
 
var defaultSWF:URLRequest = new URLRequest("swfs/introduction.swf");
 
theLoader.load(defaultSWF);
theLoader.addEventListener( Event.INIT, on_Init );
 
 
function btnClick(event:MouseEvent):void
{
	//removeChild(theLoader);
	var newSWFRequest:URLRequest=new URLRequest("swfs/" + event.target.name + ".swf"); 
	
	theLoader.load(newSWFRequest);
	theLoader.x = Xpos;
	theLoader.y = Ypos;
	addChild(theLoader);
}
 
function on_Init( evt:Event ):void
{
	var theLabel:String = labelArray[ evt.target.name ];
	swfLoaded(theLabel);
}
 
function swfLoaded(theLabel:String = null):void
{
	removeChild(theLoader);
	var clip:MovieClip = theLoader as MovieClip;
	clip.gotoAndPlay(theLabel);
}
 
//Main Btn listeners:
letter.addEventListener(MouseEvent.CLICK, btnClick);
who.addEventListener(MouseEvent.CLICK, btnClick);
what.addEventListener(MouseEvent.CLICK, btnClick);
audiences.addEventListener(MouseEvent.CLICK, btnClick);
community.addEventListener(MouseEvent.CLICK, btnClick);
//financials.addEventListener(MouseEvent.CLICK, btnClick);
communityHospitals.addEventListener(MouseEvent.CLICK, btnClick);
communitySupport.addEventListener(MouseEvent.CLICK, btnClick);
 

Open in new window

why are you trying to remove something?  that code is not needed, because you are still using the loader...

when you try to remove something, you can't use the code again.... and this is why your code keeps breaking...

      removeChild(theLoader);
      var clip:MovieClip = theLoader as MovieClip;
      clip.gotoAndPlay(theLabel);

this code can't work, because you have just removed theLoader... so you can't use something that just got deleted...


try getting what i gave you to work first, before you start adding in new buttons and functionality.


rp / ZA
Thanks.
What I posted above is working except for the external clips loading to a specific frame, and the default swf doesn't load at all.

As I said in my previous comment, I did try to get your latest suggestion to work first. It did not work.
I have removed removeChild (again) and it still isn't working.

I've added nothing additional since several posts ago (added 2 event listeners). What I did was move the addChild to the btnClick, which actally made the swf's load.

I appreciate it if you can give me guidance on the default swf loading, and on how to make some of the swf's load at a particular frame.
Thanks


whoops...  addChild was supposed to go in the on_init() event listener...  but yeah, you can put it in the button click code as well, because it just tells the application to add the loader to the stage when it is completely loaded...


below is the updated code...  forgot to position the default SWF and add it to the display list.  see if that works.  


FYI... i would recommend not loading your assets and placing them at float points... like 10.9  Flash doesn't really like that, and migth skew the images.


rp / ZA

// AS 3.0
 
stop();
 
var Xpos:Number=10.9;
var Ypos:Number=120;
var swf:MovieClip;
var loader:Loader = new Loader();
 
var labelArray:Array = new Array();
labelArray["introduction"] = "1";
labelArray["letter"] = "frame7";
labelArray["who"] = "whoFrame9";
labelArray["what"] = "frame1";
labelArray["audiences"] = "start";
labelArray["community"] = "frame8";
labelArray["communityHospitals"] = "hospitals";
labelArray["communitySupport"] = "support";
 
 
 
var theLoader:Loader=new Loader();
theLoader.addEventListener( Event.INIT, on_Init );
 
var defaultSWF:URLRequest = new URLRequest("swfs/introduction.swf");
 
theLoader.load( defaultSWF );
theLoader.x = Xpos;
theLoader.y = Ypos;
addChild(theLoader);
		
function btnClick(event:MouseEvent):void
{
        var newSWFRequest:URLRequest = new URLRequest("swfs/" + event.target.name + ".swf"); 
        
        theLoader.load(newSWFRequest);
        theLoader.x = Xpos;
        theLoader.y = Ypos;
        addChild(theLoader);
}
 
function on_Init( evt:Event ):void
{
        var theLabel:String = labelArray[ evt.target.name ];
        swfLoaded(theLabel);
}
 
function swfLoaded(theLabel:String = null):void
{
        var clip:MovieClip = theLoader as MovieClip;
        clip.gotoAndPlay(theLabel);
}
 
//Main Btn listeners:
 
letter.addEventListener(MouseEvent.CLICK, btnClick);
who.addEventListener(MouseEvent.CLICK, btnClick);
what.addEventListener(MouseEvent.CLICK, btnClick);
audiences.addEventListener(MouseEvent.CLICK, btnClick);
community.addEventListener(MouseEvent.CLICK, btnClick);
//financials.addEventListener(MouseEvent.CLICK, btnClick);
communityHospitals.addEventListener(MouseEvent.CLICK, btnClick);
communitySupport.addEventListener(MouseEvent.CLICK, btnClick);
 
 

Open in new window

BTW...  the values passed to the associative array are the names of the frame labels...

so you need to change the values that are being set for the frame labels in the below...


labelArray["introduction"] = "1";
labelArray["letter"] = "frame7";
labelArray["who"] = "whoFrame9";
labelArray["what"] = "frame1";
labelArray["audiences"] = "start";
labelArray["community"] = "frame8";
labelArray["communityHospitals"] = "hospitals";
labelArray["communitySupport"] = "support";



rp / Zone Advisor
Thanks very much. I will try all of this out once I'm back at my desk. Do you have any alternate suggestions for positioning the loaded swf's? I just thought x/y/ values were pretty much my only option to get everything loaded in the right spot on stage...
no, x,y positioning is really all you have...   but you can scale, adjust the alpha and tint... all sorts of other options...


rp / ZA
Thanks, RP. Everything loads, (used your code verbatim this time), and I changed my frame labels as necessary, but none of the swf's lod to the specific requested frame.
can you post all of your code, so I can see what you are currently working with...


rp / ZA
Here's the code. The only buttons I've been testing so far ar communityHospitals thru frameTest.
Default loads fine, all other movies that shouls load at a specific frame only load from the first frame.
Thanks for your help!
stop();
 
var Xpos:Number=10.9;
var Ypos:Number=120;
var swf:MovieClip;
var loader:Loader = new Loader();
 
var labelArray:Array = new Array();
labelArray["introduction"] = "1";
labelArray["letter"] = "frame7";
labelArray["who"] = "whoFrame9";
labelArray["what"] = "frame1";
labelArray["audiences"] = "sampleFrame";
labelArray["audienceSample"] = "sampleFrame";
labelArray["community"] = "frame8";
labelArray["communityHospitals"] = "hospitals";
labelArray["communitySupport"] = "5";
labelArray["frameTest"] = "sampleFrame";
 
 
 
var theLoader:Loader=new Loader();
theLoader.addEventListener( Event.INIT, on_Init );
 
var defaultSWF:URLRequest = new URLRequest("swfs/introduction.swf");
 
theLoader.load( defaultSWF );
theLoader.x = Xpos;
theLoader.y = Ypos;
addChild(theLoader);
		
function btnClick(event:MouseEvent):void
{
        var newSWFRequest:URLRequest = new URLRequest("swfs/" + event.target.name + ".swf"); 
        
        theLoader.load(newSWFRequest);
        theLoader.x = Xpos;
        theLoader.y = Ypos;
        addChild(theLoader);
}
 
function on_Init( evt:Event ):void
{
        var theLabel:String = labelArray[ evt.target.name ];
        swfLoaded(theLabel);
}
 
function swfLoaded(theLabel:String = null):void
{
        var clip:MovieClip = theLoader as MovieClip;
        clip.gotoAndPlay(theLabel);
}
 
//Main Btn listeners:
 
//letter.addEventListener(MouseEvent.CLICK, btnClick);
//who.addEventListener(MouseEvent.CLICK, btnClick);
//what.addEventListener(MouseEvent.CLICK, btnClick);
//audiences.addEventListener(MouseEvent.CLICK, btnClick);
//community.addEventListener(MouseEvent.CLICK, btnClick);
//financials.addEventListener(MouseEvent.CLICK, btnClick);
communityHospitals.addEventListener(MouseEvent.CLICK, btnClick);
communitySupport.addEventListener(MouseEvent.CLICK, btnClick);
audienceSample.addEventListener(MouseEvent.CLICK, btnClick);
frameTest.addEventListener(MouseEvent.CLICK, btnClick);
 
////////////////////////////////////////////////

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of rascalpants
rascalpants
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, RP. I think this will work. I really appreciate the help.