Skip to main content
saratogacoach
Inspiring
July 15, 2014
Answered

AS3 pass variables-values from loaded external swf to parent swf

  • July 15, 2014
  • 2 replies
  • 4723 views

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,

This topic has been closed for replies.
Correct answer kiran1989

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

2 replies

kiran1989
Inspiring
July 15, 2014

Hi Saratogacoach,

I made a small example for you hope you understandard the code.

You can download the example frome here:

http://d.pr/f/4yK7R

Thanks

Kiran

saratogacoach
Inspiring
July 15, 2014

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.

kglad
Community Expert
Community Expert
July 15, 2014

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.


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.

kiran1989
kiran1989Correct answer
Inspiring
July 15, 2014

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

saratogacoach
Inspiring
July 15, 2014

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,