Actionscript & Shared functioins

//Actionscript & Shared functioins

I am working with some AS classes trying to become more OOP in nature, and so I am testing keeping everything organized in separate folders according to tasks. You know the drill I like to keep the AS classes in my com folder and inside their separate different classes by what they might do. Shapes in one folders, text in another and so on.

I have been trying this shapes example I came up with with calls another AS class. This works fine when they are both in com, but when they are bother in their own separate folder I get an error.

This is the example when everything is in the same folder

InheritingClasses.as


package {

	import flash.display.Sprite;

	import flash.filters.GlowFilter;

	import flash.filters.DropShadowFilter;

	import flash.filters.BlurFilter;

	import Triangles;



	public class InheritingClasses extends Triangles {

		public function InheritingClasses() {

			triangle1.filters = [new GlowFilter(0xFF6699, .75, 25, 25, 2, 2, false, false)];

			triangle2.filters = [new DropShadowFilter(5, 45, 0x666666, 1, 5, 5, 1, 1, false, false, false)];

			triangle3.filters = [new BlurFilter(5, 5, 2)];

		}

	}

}



Triangles.as


package {

	import flash.display.Sprite;

	import flash.display.Shape;



	public class Triangles extends Sprite{

		internal var triangle1:Shape = new Shape();

		internal var triangle2:Shape = new Shape();

		internal var triangle3:Shape = new Shape();



		public function Triangles(){

			triangle1.graphics.lineStyle(2, 0x000000);

			triangle1.graphics.beginFill(0xFF0000, 1);

			triangle1.graphics.moveTo(200, 100);

			triangle1.graphics.lineTo(200, 100);

			triangle1.graphics.lineTo(275, 225);

			triangle1.graphics.lineTo(125, 225);

			triangle1.graphics.endFill();

			addChild(triangle1);



			triangle2.graphics.lineStyle(2, 0x000000);

			triangle2.graphics.beginFill(0x00FF00, 1);

			triangle2.graphics.moveTo(375, 50);

			triangle2.graphics.lineTo(375, 50);

			triangle2.graphics.lineTo(525, 50);

			triangle2.graphics.lineTo(450, 175);

			triangle2.graphics.endFill();

			addChild(triangle2);



			triangle3.graphics.lineStyle(2, 0x000000);

			triangle3.graphics.beginFill(0x0000FF, 1);

			triangle3.graphics.moveTo(275, 375);

			triangle3.graphics.lineTo(275, 375);

			triangle3.graphics.lineTo(400, 250);

			triangle3.graphics.lineTo(100, 350);

			triangle3.graphics.endFill();

			addChild(triangle3);

		}

	}

}



of course I set the doc class to “InheritingClasses”

Now when I create the com folder, and put the InheritingClasses.as in “classes and triangles.as in “Shapes” I got a weird error.

Attempted access of inaccessible property triangle1 through a reference with static type com.classes:InheritingClasses.



Attempted access of inaccessible property triangle2 through a reference with static type com.classes:InheritingClasses.



Attempted access of inaccessible property triangle3 through a reference with static type com.classes:InheritingClasses.

Now why would this fail when I separate the as files into com/shapes for triangles.as and com/classes for the main class? Of course I updated everything to reflect this change.

I asked this question on Linkedin and Jeff Fall tells me to use public static in the function,and I will try this tonight.

By | 2009-03-26T01:57:16+00:00 March 26th, 2009|Actionscript 3.0|4 Comments

About the Author:

4 Comments

  1. Rich Shupe March 30, 2009 at 1:02 am

    Johnny,

    Assuming this change is complete and correct: “of course I updated everything to reflect this change,” the reason this is happening is because of namespaces. In the code you cited above, you’re using internal for your instance properties. That namespace gives access to your properties to all classes in the same package. Therefore, while all your classes are in com, or hfug, or any other package directory, the InheritedClasses class will recognize the instance names triangle1, triangle2, etc.

    However, when you move the classes to separate packages, no matter what they may be, InheritedClasses doesn’t recognize those instances. If you then changed “internal” to “protected”, which grants access to the class and subclass, the fact that InheritedClasses extends Triangles means the former will know about the instances.

    It will be a great exercise to change the name spaces among public, private, protected, and internal and see what changes you have to make to the classes to get the example to work. Then you might consider changing the structure around a bit. It seems a little like putting the cart before the horse. If you decide to reorganize, however, do it last. You’ve fallen into a great test case to learn about the four namespaces.

  2. John Barrett March 30, 2009 at 10:34 pm

    Hi Rich,

    Thanks so much for your comment!

    Yep, when I used “protected” or “public” it worked fine. As for the “private”,”internal” name spaces did not. I am very interested in seeing what I have to do to get them to working in the same way.

    This is interesting as doing this example will teach me about name spaces in AS 3!

    When I figure this out, I think that it will make a very interesting blog post`-`

  3. Brock Saetern March 31, 2010 at 2:13 pm

    Hi, i must say fantastic site you have, i stumbled across it in Bing. Does you get much traffic? If you wishto swap the links with us please let me know.

  4. watch twilight April 7, 2010 at 6:52 pm

    howdy fabulous little blog you have there πŸ˜‰ I work with the same exact theme on mine but for whichever cause it looks to load speedier on this web site despite the fact that this site is carrying a whole lot more material. Are you using any sort of plugins or widgets that quicken it up? Do you think you could show the titles so maybe I would be able to use these on my own site so twilight new moon followers could watch twilight new moon online trailers and clips a lot quicker I’d personally always be grateful – thanks ahead of time πŸ™‚

Leave A Comment