Copy link to clipboard
Copied
Hi,
I am working on an eLearning project that uses a main SWF (SWF1) and a second SWF (SWF2) with 2 quizzes in it. Published to AIR for Desktop and Android, SWF1 and SWF2 are packaged together in the AIR desktop or AIR APK exports.
I can use a loader in SWF1 to call and run SWF2. What I am having difficulty with, need some help, is how to get the 2 scores from the 2 quizzes in SWF2 and load these values (each is a Number) back into SWF1. Once back in SWF1, they will be written to String and then displayed in a text box using textbox.text.
While I've searched online and found examples for sending variables from parent to child SWF's, so far not able to find an understandable/useable AS3 script example for getting variables from child to parent SWF.
Any help appreciated.
Best Wishes,
1 Correct answer
The better way to communicate between parent and child swfs is through events. Dispatch an event from child with data to be send and catch the event at parent level. This flow is common and very easy to handle
Copy link to clipboard
Copied
The better way to communicate between parent and child swfs is through events. Dispatch an event from child with data to be send and catch the event at parent level. This flow is common and very easy to handle
Copy link to clipboard
Copied
Hi Kiran
Thank you for your reply and helpful suggestions. I am an amateur at scripting in AS3, so it would be a big help if I had an example, if possible.
Best Wishes,
Copy link to clipboard
Copied
Hi Saratogacoach,
I made a small example for you hope you understandard the code.
You can download the example frome here:
Thanks
Kiran
Copy link to clipboard
Copied
Thank you, Kiran.
I understand the code but will need to study to figure out how to use it. I think that the "child.swf" has the code for what I would call my SWF2: the second SWF (SWF2) with the 2 quizzes from which I want to send the 2 quiz scores-score1 and score2 back to the main SWF (which I am calling SWF1) that has launched the second SWF. Then the main SWF (SWF1) can convert the scores (numbers) to String and display them for the quiz taker in 2 text boxes in SWF1.
I will try to get it to work and update if I am successful.
Thank you very much.
Copy link to clipboard
Copied
I have been trying to add the code to my child SWF, but am getting an error:
Scene 1, Layer 'Layer 55', Frame 9, Line 110 1180: Call to a possibly undefined method AppEvent.
Scene 1, Layer 'Layer 55', Frame 9, Line 110 1120: Access of undefined property AppEvent.
This is an AIR project done in Flash Professional, and so I am debugging in AIR desktop mode. I am wondering if these events do not work in AIR?
Copy link to clipboard
Copied
Can you show me the code
Copy link to clipboard
Copied
Hi Kiran,
Unfortunately, the parent has a couple of hundred frames with code, and the child has about 12 frames with code. Too long and "messy" to share. (I understand that this makes it more difficult to help. But, I'll just need to find a way to get the 2 score results back into SWF1 from SWF2. Another option, since the 2 scores are posted to a DB, is to write a small ASP script to access the DB and load the variables into SWF1 via the ASP webpage.)
Copy link to clipboard
Copied
OK, I may be getting it to work. I had left the AS3 file out of the folder, so I kept getting error messages that this AppEvent class was missing.
I think I may be able to get it to work using it correctly.
Much thanks.
Copy link to clipboard
Copied
swf-to-swf communication
http://kb2.adobe.com/community/publishing/918/cpsid_91887.html
Copy link to clipboard
Copied
Thank you, kglad.
I've begun to read it and try to understand it. May take a while. But learning a lot.
Thank you, again, kiran1989.
Before getting this latest posting, I had been trying to get (kiran1989's) code to work in my project, with some success. The only issue with using this approach is how to send more than one variable back to SWF1. I can send only one variable (score1) back currently, using
function onClick(event:MouseEvent):void
{
dispatchEvent(new AppEvent(AppEvent.CHILD_DATA,score1,true));
}
If I want to send both variables score1 and score2 back (each to populate a different text box), not sure how to get both back?
Copy link to clipboard
Copied
you really shouldn't pass data in events unless you're careful to clean up afterwards, but you could use:
function onClick(event:MouseEvent):void
{
dispatchEvent(new AppEvent(AppEvent.CHILD_DATA,{var1:score1,var2:score2},true));
}
Copy link to clipboard
Copied
Thank you, again, kglad.
Almost there. The variables are now correctly saved and can be traced. Haven't been able to get back from SWF2 to SWF1 (and view if the 2 text boxes are correctly populated with the 2 variables), because I keep getting an error, I think related to my script in SWF2 to return to the SWF1 timeline, frame 2 (loading frame for SWF2).
Here's the code and error:
//child SWF
var score1:Number; //first score;actually appears in script layer that extends to this frame and works
var rbselectedT:Number; //second score;actually appears in script layer that extends to this frame and works
bTemp5.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void
{
dispatchEvent(new AppEvent(AppEvent.CHILD_DATA,{var1:score1,var2:rbselectedT},true));
MovieClip(parent.parent).gotoAndStop(2);//to return to parent SWF--I think this may be the problem;I think I had used this earlier with success but in a different SWF2 frame
}
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@2afa161 to flash.display.MovieClip.
at Hotspot13_fla::MainTimeline/onClick()[Hotspot13_fla.MainTimeline::frame9:127]
So, need to figure out, once scores are sent back to SWF1, how to return the user to SWF1? So far, keep getting an error.
Copy link to clipboard
Copied
if parent.parent is the stage, try using just parent.
but you really should define variables that reference the main timeline in both swfs so you can see exactly what you're doing:
var tl_main:MovieClip=this; // from the document class of the loading swf or the main timeline of the loading swf
var tl_loaded:MovieClip=this; // from the document class of the loaded swf or the main timeline of the loaded swf.
Copy link to clipboard
Copied
Hi,
Thank you for these suggestions. I tried first using parent instead of parent.parent, but same error. Not sure why this is happening in this frame. I had used MovieClip(parent.parent).gotoAndStop(3); earlier, on another frame, and as I recall it worked (I was going to frame 3 instead of the loading frame, frame 2. Wonder if this made a difference?)
Not adverse to the better method you mention, but far from sure how to actually re-code to accomplish this? I can read some more from your blog referenced above.
I'll keep trying.
Copy link to clipboard
Copied
use the trace statement to debug:
trace(this,this.parent,this.parent.parent);
Copy link to clipboard
Copied
Looks like I am getting:
[object MainTimeline] [object Stage] null
I didn't call the return to SWF1 script (MovieClip(parent.parent).gotoAndStop(2);).
Copy link to clipboard
Copied
if you're trying to direct the current main timeline to frame 2, use:
MovieClip(this).gotoAndStop(2);
Copy link to clipboard
Copied
Hi kglad,
Thank you for your continued help.
I think that when I debugged before and included the debug results, I had not debugged with SWF2 loaded by SWF1.
When I now do this correctly, I get:
[object MainTimeline] [object MainTimeline] [object Stage]
So, to summarize, when I am viewing SWF2, it is being loaded and run from SWF1. When I use:
MovieClip(this).gotoAndStop(2);
This takes me to SWF2 frame 2. Where I need to go (after the user completes 2 quizzes in SWF2) is back to SWF1 frame 2, where 2 text boxes will display the 2 score variables (obtained from the quizzes in SWF2). Please note that it is also SWF frame 2 that has the launch button that loads SWF2, in case this matters.
Sorry for the confusion.
Best Wishes,
Copy link to clipboard
Copied
the loader's content property (cast as a movieclip) is a reference to the loaded swf's main timeline.
Copy link to clipboard
Copied
Hi,
Still stuck. Can't get SWF2 to go back to SWF1 frame 2.
SWF1, based on Kiran's AS file with the class AppEvent (class is not called in SWF1 until frame 2, and is called in SWF2 frame 9) loads SWF2 using:
//SWF1, frame 2
stop();
import flash.display.Loader;
expB100.addEventListener(MouseEvent.CLICK, L1, false, 0, true);
function L1(e:MouseEvent):void {
loaderL1.load(new URLRequest("Hotspot13.swf"));
loaderL1.contentLoaderInfo.addEventListener(Event.COMPLETE, onChildLoaded);
addChild(loaderL1);
expB100.visible = false;
}
function onChildLoaded(evt:Event)
{
var child:DisplayObject = evt.target.content as DisplayObject;
child.addEventListener(AppEvent.CHILD_DATA, getChildData);
addChild(child);
}
function getChildData(evt:AppEvent):void
{
trace(evt.data.var1,evt.data.var2);
tTest.text = String(evt.data.var1);
tTest2.text = String(evt.data.var2);
}
I wonder if there is some confusion between MovieClip and DisplayObject when SWF2 tries to return to SWF1 frame 2, using MovieClip(parent.parent).gotoAndStop(2); and gets an error?:
Error #1034: Type Coercion failed: cannot convert flash.display::Stage@2f1ef71 to flash.display.MovieClip.
at Hotspot13_fla::MainTimeline/onClick()[Hotspot13_fla.MainTimeline::frame9:124]
Hotspot13 is SWF2; SWF2 frame 9 uses the following to try to return to SWF1 frame 2, causing the above error:
function onClick(event:MouseEvent):void
{
trace(this,this.parent,this.parent.parent);
dispatchEvent(new AppEvent(AppEvent.CHILD_DATA,{var1:score1,var2:rbselectedT},true));
MovieClip(parent.parent).gotoAndStop(2);
}
Or maybe the AppEvent.as file is imported in frame 1 when it should be imported in another later frame like SWF1 frame 2 where it is first used?
Best Wishes,
Copy link to clipboard
Copied
Hi,
Can you provide me your project. I will help you in that way better or else tell me your project process. Where you handling Swf1, swf2, swf3
Copy link to clipboard
Copied
Hi Kiran,
Thank you for your help. As I mentioned above in another posting, the project is large, and I can not provide it.
The main/parent SWF, SWF1, has a couple of hundred frames with multiple quizzes, learning materials. But, before the first quiz is taken in SWF1, 2 other quizzes need to be completed by the user in SWF2, the child swf. So, in SWF1, frame 2 a button is used to load and run SWF1. When SWF1 loads and opens, the user takes the 2 quizzes and get 2 scores (these 2 score variables, with your help with the script, can now be sent to SWF1, using the dispatch event in SWF2 and the AppEvent listener in SWF1). After completing these 2 quizzes in SWF2, sending back the 2 score variables to SWF1, then I would like the user to return back to SWF1, frame 2 (the SWF1 loading frame where the button loaded SWF2). From there, they can go forward to the next frame and take the next SWF1 quiz.
I had originally put all of the quizzes, now in SWF1 and SWF2 in the same SWF, but because of very fast timing like 400 ms or 600 ms, there were too many functions, code and this ruined the timing. So I separated 2 big quizzes and put them, instead, into a new SWF: SWF2. But now I am stuck trying to load SWF2 from inside SWF1, get back the 2 quiz scores into SWF1, and return the user from SWF2 back to SWF1, so they can go to the other quizzes.
I hope this is a better explanation. Please ask for any additional clarification, information.
Again, thank you for your help.
Kind Regards,
Copy link to clipboard
Copied
Hi Kiran,
I just sent you a message with a download link for the 2 FLA's for this project (made smaller by removing unnecessary frames, materials-I included your AppEvent class AS file that you provided). Thank you for your help. Please let me know if you need any additional information or clarification.
Kind Regards,
Copy link to clipboard
Copied
Hi,
Still cannot get SWF2 frame 9 to go back to SWF1 frame 2 (or frame 3). SWF2 frame 9 has DispatchEvent which returns data from SWF2 to SWF1. But after data is returned, then the user needs to go back to SWF1 to continue in SWF1.
Tried
MovieClip(parent.parent).gotoAndStop(2);
or
MovieClip(parent.parent).gotoAndPlay(3);
But these cause error messages:
error message from MovieClip(parent.parent).gotoAndStop(2);
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@31a3161 to flash.display.MovieClip.
at Hotspot13_fla::MainTimeline/onClick()[Hotspot13_fla.MainTimeline::frame9:124]

