Copy link to clipboard
Copied
I have a function in a child that I need to have executed by the parent but I can't get it to work. Does anyone know how to call the function in a child from a parent?
What I'm doing is having the parent load a few buttons and I want to re-use the button swf, but have the parent provide the text for the button.
Here's how I have it laid out:
main.swf - main container for everything
- - button_page.swf - this is the page that has the buttons on it
- - - - button.swf - the button image with dynamic text field
So the button_page.swf is trying to execute a function on button.swf, which is called "populate_button". It's also passing the text in it so the function should be run like "populate_function("Back")". I just can't get the call to the function correctly. I've tried many different things including:
btn_back.populate_button("BACK");
the instance of the button.swf is called "btn_back".
Any help would be greatly appreciated. Thanks!
Aaron
if I am not mistaken ....
in AS3 ... the only "proper" inter swf comm is w/ local connection ... parent has been deprecated .. save for XML nodes ...
You are mistaken ![]()
2 swf's can communicate with eachother just fine. Just make sure they're actually loaded before attempting to communicate.
You only need a netconnection for cross-avm communication: AVM0 (AS1/AS2) to AVM3 (AS3) and vice versa.
VIVIsecVI, show us the code that actually loads the button swf.
Copy link to clipboard
Copied
is there a loader in main.swf loading button_page.swf? is there a loader in button_page.swf loading button.swf?
if yes, then if populate_button() is on the main timeline of button.swf, use:
MovieClip(button_pageLoader.content).populate_buttion()
to call button.swf's function from the timeline that contains its loader.
Copy link to clipboard
Copied
Sorry, to clarify, the function is on the button.swf file. It is:
button.swf:
function populate_button(btn_text:String):void {
nav_btn_text.autoSize = "center";
nav_btn_text.text = btn_text;
}
The parent is where I'm trying to execute the function so the button can populate itself with the correct data.
The parent (button_page.swf) has:
if (MovieClip(this.parent.parent).prev_clip > 0) {
var btn_back = new Loader();
btn_back.x=100;
btn_back.y=500;
addChild(btn_back);
}
So after the button movie clip is added, I want the parent to then populate it's child with the proper text by running the function on the child.
Thanks!
Copy link to clipboard
Copied
do you really have swf files? if so, are you using the loader class to load one or more swfs?
Copy link to clipboard
Copied
i now see you do have a loader but that's for a different swf.
Copy link to clipboard
Copied
Sorry, that loader is for the button. It's actually called "nav_button.swf" not "button.swf". My mistake!
Copy link to clipboard
Copied
Yes, the swf files exist and the button shows up but it has the text put into the dynamic text field on the timeline instead of replacing it with what is being fed in through the function.
It's using the Loader class to load the button.swf (and all other movie clips are also being loaded using the Loader class as well).
So the only purpose of the function is to feed the correct text into the button instance since multiple buttons will use the same button.swf file.
Copy link to clipboard
Copied
is this AS2 or AS3 ...
I thought parent was deprecated in AS3 .. ??
assuming your parent.parent ref works ..
button.swf:
var cc:LocalConnection = new LocalConnection()
cc.allowDomain("*")
cc.client = this
function populate_button(btn_text:String):void {
nav_btn_text.autoSize = "center";
nav_btn_text.text = btn_text;
}
button_page.swf
var cc:LocalConnection = new LocalConnection()
cc.connect("_cCom")
if (MovieClip(this.parent.parent).prev_clip > 0) {
var btn_back = new Loader();
btn_back.x=100;
btn_back.y=500;
addChild(btn_back);
cc.send("_cCom","populate_button",btn_text)
}
Copy link to clipboard
Copied
you need to declare the local connection on both swf's ...
Copy link to clipboard
Copied
The parent thing does work, but I've temporarily disabled the if statement for faster testing. It's in Action Script 3
I tried making the changes you recommended but I'm still getting errors:
Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.LocalConnection was unable to invoke callback populate_button. error=ReferenceError: Error #1069: Property populate_button not found on flash.net.LocalConnection and there is no default value.
Action Script 3 can be a pain sometimes!!!
Copy link to clipboard
Copied
try locating the local connection statements in the child after you declare the function ...
this could be the blind leading ....
if your watching the forum ... you can see I'm looking for assist on just this matter ... though I do have working code ...
if I am not mistaken ....
in AS3 ... the only "proper" inter swf comm is w/ local connection ... parent has been deprecated .. save for XML nodes ...
Copy link to clipboard
Copied
Thanks for your suggestion with the local connection. Parent was the only way I found where I could update variables and run functions on different movie clips at different levels, but I will look into local connection more and perhaps replace the parent references if it works and is the proper way of doing it.
I'll let you know if I get it working.
Thanks!
Copy link to clipboard
Copied
make note of the conn.client statement ... it's not well doc'd in the exaples in the help file ..
Copy link to clipboard
Copied
I've got to bounce ..
I'll touch base when I return ... but you'll have it working by then ... ![]()
Copy link to clipboard
Copied
if I am not mistaken ....
in AS3 ... the only "proper" inter swf comm is w/ local connection ... parent has been deprecated .. save for XML nodes ...
You are mistaken ![]()
2 swf's can communicate with eachother just fine. Just make sure they're actually loaded before attempting to communicate.
You only need a netconnection for cross-avm communication: AVM0 (AS1/AS2) to AVM3 (AS3) and vice versa.
VIVIsecVI, show us the code that actually loads the button swf.
Copy link to clipboard
Copied
Thanks Muzak, that was the problem!
I had to add a listener that made sure the clip was loaded before trying to change any variables in the clip or run any functions. Here's what I did:
On the clip loader I added:
addChild(btn_back);
btn_back.contentLoaderInfo.addEventListener(Event.COMPLETE, load_back);
So after the child is added, I have a listener that executes a function once the clip is loaded. Then I have the following for the "load_back" function which is run when the clip is loaded:
function load_back(event):void {
trace("Load Complete");
MovieClip(btn_back.content).nav_btn_text.text = "BLAH";
}
So the function then updates the text field of the child directly without using a function within the child like I was trying to do. This works perfectly!
Thanks a ton everyone!
Aaron
Copy link to clipboard
Copied
%^%$#@# there goes my perfect record !!!!
THANKS for pointing out my error Muzak !!!
VIVIsecVI ... glad to hear your up and running ... sorry if I steered you down a wrong path ...
Copy link to clipboard
Copied
use the local connection object ..
in the main swf ..
var conn:LocalConnection = new LocalConnection()
conn.connect("_cCom")
conn.send("_cCom","name of function",function params)
in the child swf
conn.allowDomain("*")
conn.client = this
function nameoffunction(function params) {
action to take
}
Copy link to clipboard
Copied
Tried this and couldn't get it working. It said:
Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.LocalConnection was unable to invoke callback populate_button. error=ReferenceError: Error #1069: Property populate_button not found on flash.net.LocalConnection and there is no default value.
On the button_page.swf I put:
var btn_back = new Loader();
btn_back.x=100;
btn_back.y=500;
addChild(btn_back);
btn_back.load(new URLRequest("nav_btn.swf"));
var conn:LocalConnection = new LocalConnection();
conn.connect("_cCom");
conn.send("_cCom","populate_button","BACK");
And on the button.swf I put:
conn.allowDomain("*");
conn.client = this;
function populate_button(btn_text:String):void {
nav_btn_text.autoSize = "center";
nav_btn_text.text = btn_text;
}
Thanks!
Copy link to clipboard
Copied
I’ll warn you I’m a bit of a noob at Flash/AS3 but I had a situation that I think was identical to yours. I had a single navigation button in the library and multiple instances on stage. I wanted the text for each instance dynamically loaded at startup and changable at runtime based on a language selection drop down.
I found out the hard way that the standard button type mc does not support dynamic text. You can put an instance of dynamic text into the button and it won’t throw an error but you can’t reference it from outside the button. The solution is to create your own mc button with off, over, and down frames and add the appropriate listeners. Once this is done the dynamic text within the mc can be changed from the parent simply by:
buttonInstanceName.dynamicTextInstanceName.text = “some new text”;
if you’d rather call a function it works the same:
buttonInstanceName.functionName(params);
Copy link to clipboard
Copied
The button is really just a movie clip, not a button. What you recommend may actually be easier and better, instead of calling a function just redefine the text on the button from the parent. The only thing is I'm still having trouble defining anything from the parent to the child. That's where I'm getting caught up.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more