View Full Version : can loaded swfs reference static methods of container.swf?
orangehaze
September 27th, 2007, 02:55 PM
Hello,
I have a Container.swf that is responsible for loading in other Content.swfs. I defined a Container.as file as the document class for Container.swf, and created a few static methods that I want to have available for the Content.swfs. I figured that after a Content.swf is loaded, I could just reference those methods like so :
Container.myStaticMethod("var1", "var2");
but I am getting a compile time error which I understand fine, but wasn't expecting it, since it wasn't thrown with AS2.
1120: Access of undefined property Ground
I also tried Stage.myStaticMethod("var1", "var2"); which yields pretty much the same result.
Do I need to import my class Container.as class in each of my content.swfs?
Thanks in advance
macerenn
October 23rd, 2007, 11:44 AM
I have been wrestling with this same problem, and the only thing I can find that works is to use LocalConnection. This lets you communicate between swfs on the same machine, even if they're running in different browsers. Well, I didn't need that, but since it ALSO lets swfs communicate with each other when one is embedded within the other, it will have to do. You invoke methods by name, send objects, what have you. I am just now getting into it, so I haven't tried a lot of the things I want to try, like passing classes from an swf into another in which the class was not defined at compile-time... What we really need here is Reflection (like in .NET and Java). Anyway, there's your easy solution. Just use LocalConnection. In the container, which has methods to be CALLED, declare and create a LocalConnection object, then call it's connect method, specifying a unique name for the connection. In the client swfs, declare and create a LocalConnection object and invoke it's send method, specifying the unique name of the connection, the method to invoke (as a string), and any arguments.
Server:
var lc:LocalConnection = new LocalConnection();
...
lc.connect("Server_LC");
function CallMe(str:String)
{
//do something with the string
}
Client:
var lc:LocalConnection = new LocalConnection();
var msg:String = new String("This is only a test");
...
lc.send("Server_LC", "CallMe", msg); //Invoke the CallMe method in the other swf
tylerdudley1986
October 23rd, 2007, 12:56 PM
orangehaze is right, you'll need to either import the container class into each of the sections and access it like 'Container.myStaticMethod("var1", "var2")', or just have the classes in the same namespace - then you don't need to import them.
TD
macerenn
October 23rd, 2007, 02:16 PM
I thought about doing that myself, but for one thing, I need to let other developers create their own swfs and provide a few standard entry points for my container to call into them (I won't have their source code). I also want to add swfs later without recompiling the container. Now, if you add the .as file to the project but don't actually use any of it the file size stays the same. As soon as you cast that loader into an instance of the imported class so that you can use it's methods by name, there is a dramatic increase in file size. Is it compiling the imported class and adding it to importing class? I dunno, not being a "real" Flash developer - I'm new to this. But I do know this... In every other OO language that I have used, if you add a class definition it goes into the compiled code so that you can get runtime type information at the least. If all you are doing is calling a function or two, and you want to be able to add in functionality later without recompiling, then LocalConnection is the way to go. Of course, thats from an old C++ COM, .NET, Java developer... If only I could find some Reflection in ActionScript.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.