PDA

View Full Version : Encapsulation


PJjerry
04-15-2008, 09:35 PM
My intension is to deploy the Tree Class as a .swc, but the "tree components"(Branch & Trunk) should be encapsulated so only the Tree class is exposed to the user. Since AS3 doesn't have a private or protected class definition, how can I achieve this? maybe using package?

I did try to use "internal class" but with no vail. Someone can point me to the right direction?

Thanks.



// *****************
// Olive Tree components
// *****************
private class Branch
{
}

private class Trunk
{
}

// *****************
// Olive Tree Class
// *****************
public class Tree
{
private var b:Branch;
private var t:Trunk;

public function Tree()
{
b = new Branch();
t = new Trunk();
addChild(b);
addChild(t);
}
}

// *****************
// Main Routine
// *****************
public class Main
{
public function Main()
{
for (var i=0; i<10; i++)
{
var tree:Tree = new Tree();
addChild(tree);
}
}

SmashingEric
04-15-2008, 09:52 PM
Setting the classes to internal should work as long as they're in seperate .as files.

See attached.

PJjerry
04-15-2008, 10:45 PM
ya your example works great :D

Looks like I am doing something stupid.... will double check my code :p

nikefido
04-15-2008, 11:45 PM
wait...since when doesn't flash allow public/private/protected for functions and classes?

SmashingEric
04-15-2008, 11:52 PM
wait...since when doesn't flash allow public/private/protected for functions and classes?

it does for functions.

Classes can only be defined using dynamic, final, internal, and public. If you try to declare a private or protected class you'll get a compiler error.

PJjerry
04-16-2008, 12:23 AM
I think I found the problem.

If the single class file .as exists solely, it works great.

However if there's a corresponding mapping Symbol in the flash gui, using the "internal" keyword will no longer work. Flash will complain:

ReferenceError: Error #1065: variable Trunk not defined.

using "public class" will work though, but that breaks the encapsulation.

is there any chance this can be fixed?

I attached a sample problematic flash .fla + .as file.

SmashingEric
04-16-2008, 12:44 AM
Ahhhh ok I see. When you link an internal class to a movieclip in the library you get the error.

Ok one work around (might not be the best, I'm open to any other suggestions) would be to keep your trunk asset seperate from the class.

Since in CS3 you can give a movieclip a class and it will auto-generate the class at compile time, for trunk and branch, I would specify class names that do not currently exist, and then Have your Trunk and Branch class not extend movieclip, and pass them a reference to their respective asset clips.

Not sure if that's very clear lol, but take a look at the files here to see what changes I made.

I don't know if it's the cleanest solution, but it works.

PJjerry
04-16-2008, 12:49 AM
man that was fast!

checking....

PJjerry
04-16-2008, 01:05 AM
ya that works, but contrarily it will expose the TrunkClip Class to users, which makes no difference if just simply public the Trunk class.