View Full Version : How to Pass Vars from external swf (as child) to parent ?
swathi84.k
May 4th, 2009, 03:13 AM
Hi ....
I am new to flash."I am unable to access the variables of child swf in parent swf after an action(button is clicked) is performed in child swf".
My problem is i had 2 swfs parent.swf and child .swf and i loaded the child.swf file in parent.swf using Loader class there is a textbox and a button in child.swf when we enter text in the textbox(say,notepanel_txt) and click on the button(say, mySubmit) the text present in textbox(notepanel_txt.text) is saved in the database using URLLoader and an variable called success(which is String type) must be available to parent. the code is as follows:
parent.fla
----------------
--------------blah... blah...
var textLdr:Loader=new Loader();
var noteUrl:String = "child.swf";
var urlReq:URLRequest=new URLRequest(noteUrl);
textLdr.load(urlReq);
addChild(textLdr);
child.fla
--------------
-------------
-------------blah... blah...
mySubmit.addaddEventListener(MouseEvent.CLICK,onCl ick);
function onClick(evnt:MouseEvent):void {
var url:String = "http://localhost:8888/addvalues"; (http://localhost:8888/addvalues%22;)
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.notevalue= notepanel_txt.text;
request.data = variables;
urlLoader.addEventListener(Event.COMPLETE, completeHandler1);
urlLoader.load(request);
function completeHandler1(event:Event):void {
var success:String = "Added Successfully"; // this String value must be passed to parent.fla from child.fla
}
}
I think, you feel free for posting reply to this post.So, spend 3 min.s of your time in filling with reply on behalf of me.
Please Help me................. Its Urgent...................
Thanks in Advance
Swathi
Shaedo
May 4th, 2009, 12:10 PM
There are two ways I know:
First just use "parent.parent.etc" to burrow up until you hit the parent swf.
add a few trace(parent.parent.parent etc) statements to work this one out. (I think you will have to cast as a movieclip at some point.
The other way is as follows:
1st your child swf dispatches an event telling the parent to get the var:
dispatchEvent(new Event("getVar", true));
then you have this in your parent swf
var success:String;
textLdr.addEventListener("getVar", getSWFVar);
function getSWFVar(event:Event):void
{
success = textLdr.content.success;
}
for the 2nd method:
If you are using CS3 or CS4 then you will need to have 'strict mode' turned off.
You will want to understand the basics of making custom event dispatchers/event listeners.
You will want to understand using the content property of your loader.
Best of luck.
Anil_kumar
May 5th, 2009, 01:06 AM
Have a look at this example and download the source files.
AS3 - Access objects from external SWF files (http://flash-workshop.blogspot.com/2009/04/access-data-from-external-or-loaded-swf.html)
http://flash-workshop.blogspot.com/2009/04/access-data-from-external-or-loaded-swf.html
Anil
Flash Workshop (http://flash-workshop.blogspot.com/)
anilkumarnd@gmail.com
swathi84.k
May 5th, 2009, 01:35 AM
Hi Shaedo,
Thanks for your reply.
Iam new to flash i tried with your reply
In Child.fla i wrote as:
------------
------------
mySubmit.addaddEventListener(MouseEvent.CLICK,onCl ick);
function onClick(evnt:MouseEvent):void {
var success:String="Added";
dispatchEvent(new Event("getVar", true));
}
In Parent.fla
var note:String;
textLdr.addEventListener("getVar", getSWFVar);
function getSWFVar(event:Event):void
{
trace("In parent ");
note = textLdr.content.success; // the control comes here from child.fla
but it is unable to access the property called success of child
}
The error which was encountered during execution is as follows:
1119: Access of possibly undefined property success through a reference with static type flash.display:DisplayObject.
How to resolve this error?
Please help me in this context.....
Thanks a lot in advance............
Swathi
There are two ways I know:
First just use "parent.parent.etc" to burrow up until you hit the parent swf.
add a few trace(parent.parent.parent etc) statements to work this one out. (I think you will have to cast as a movieclip at some point.
The other way is as follows:
1st your child swf dispatches an event telling the parent to get the var:
dispatchEvent(new Event("getVar", true));
then you have this in your parent swf
var success:String;
textLdr.addEventListener("getVar", getSWFVar);
function getSWFVar(event:Event):void
{
success = textLdr.content.success;
}
for the 2nd method:
If you are using CS3 or CS4 then you will need to have 'strict mode' turned off.
You will want to understand the basics of making custom event dispatchers/event listeners.
You will want to understand using the content property of your loader.
Best of luck.
swathi84.k
May 5th, 2009, 01:52 AM
Hi Anil Kumar,
Thanks for your help...
I am new to flash.Presently, i am using flash cs3 but the code available in the url given by you is in cs4.So, it is unable to open in cs3 (unexpected file format) can you please help me by providing cs3 zip file so that i can resolve my issue.
Thanks a lot in Advance........
Waiting for your reply....
Swathi
Have a look at this example and download the source files.
AS3 - Access objects from external SWF files (http://flash-workshop.blogspot.com/2009/04/access-data-from-external-or-loaded-swf.html)
http://flash-workshop.blogspot.com/2009/04/access-data-from-external-or-loaded-swf.html
Anil
Flash Workshop (http://flash-workshop.blogspot.com/)
anilkumarnd@gmail.com
Anil_kumar
May 5th, 2009, 02:04 AM
please find attached "accessExternalSwf_CS3.zip" (27.8 KB)
Hi Anil Kumar,
Thanks for your help...
I am new to flash.Presently, i am using flash cs3 but the code available in the url given by you is in cs4.So, it is unable to open in cs3 (unexpected file format) can you please help me by providing cs3 zip file so that i can resolve my issue.
Thanks a lot in Advance........
Waiting for your reply....
Swathi
Shaedo
May 5th, 2009, 05:06 AM
Hi Swathi
Looks like you have some links to tutorials which probably have better solutions than mine, and I certainly won't be offended if you ignore the rest of this post!....
....But just incase you are getting the 1119 error for one of two reasons.
The most common reason (sounds like this is not your problem) is that you have not turned off 'strict mode' in CS3. This is important because, in compiling with strict mode on, flash will try and verify the existance of all the variables you use. As the success variable is not present in your parent swf (its in your child swf) it will throw the 1119 error.
The other reason is that you need to direct your textLdr.content.....success correctly.
the code textLdr.content (where textLdr is your loader) directs flash to look at the content of what ever is in your loader. If (as in your case) the content is a child swf then it can further start looking through the structure of your child swf. So after the .content you will need to have it point through any childs to one that contains the actual variable (or to be precise the reference to the variable). It might be somthing along the lines of textLdr.content.myObject.success;
If you run into real trouble I would be happy to have a look at your code if you want to post your .fla and .as files but as I say looks like someone posted a good tutorial and I think they are always the best way to go.
Best of luck.
swathi84.k
May 5th, 2009, 06:54 AM
Hi Anil,
Thanks a lot for sending zip file.
Actually my problem is after an clickListener is performed successfully in child (it saves data in database) it must pass the success message from child(which is in urlLoader.addEventListener(Event.COMPLETE, completeHandler1); ) to parent.
Anyway thanks a lot Anil for responding to my post this is also partially helpful for me.
I got solved the issue i am posting the code which i had used
In Child.fla
--------------
--------------
var success:TextField = new TextField();
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
var request:URLRequest = new URLRequest(url);
urlLoader.addEventListener(Event.COMPLETE, completeHandler1);
urlLoader.load(request);
function completeHandler1(event:Event):void {
success.text="Added Successfully";
MovieClip(parent.parent).success_msg.text=success. text;
dispatchEvent(new Event("getVar", true));
}
In Parent.fla
--------------
--------------
textLdr.addEventListener("getVar", getSWFVar);
function getSWFVar(event:Event):void
{
trace("success message is:"+success_msg.text);
}
Thanks,
Swathi
please find attached "accessExternalSwf_CS3.zip" (27.8 KB)
swathi84.k
May 5th, 2009, 07:11 AM
Hi Shaedo,
I had Turned off the StrictMode.
Your suggestion helped me and is working fine.
Thanks a lot.
Swathi:)
Hi Swathi
Looks like you have some links to tutorials which probably have better solutions than mine, and I certainly won't be offended if you ignore the rest of this post!....
....But just incase you are getting the 1119 error for one of two reasons.
The most common reason (sounds like this is not your problem) is that you have not turned off 'strict mode' in CS3. This is important because, in compiling with strict mode on, flash will try and verify the existance of all the variables you use. As the success variable is not present in your parent swf (its in your child swf) it will throw the 1119 error.
The other reason is that you need to direct your textLdr.content.....success correctly.
the code textLdr.content (where textLdr is your loader) directs flash to look at the content of what ever is in your loader. If (as in your case) the content is a child swf then it can further start looking through the structure of your child swf. So after the .content you will need to have it point through any childs to one that contains the actual variable (or to be precise the reference to the variable). It might be somthing along the lines of textLdr.content.myObject.success;
If you run into real trouble I would be happy to have a look at your code if you want to post your .fla and .as files but as I say looks like someone posted a good tutorial and I think they are always the best way to go.
Best of luck.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.