Neal Davis
Neal Davis

Reputation: 2008

AS3... How to have multiple public methods in a class in a separate package?

edit

Yep! Just adding the public keyword fixed it.

This (Adobe help site) was partly where my confusion came from:

and only one package-level declaration per file can use the public attribute, whether that declaration is a class, variable, function, or namespace.

but also because I've always gotten errors saying something about only one public function allowed if I tried declaring more than the constructor function as public (which I don't seem to be able to replicate right now, so I know it seems dim of me to not have tried putting public there, but I've just never been able to before). My lack of deeper understanding is showing. Bonus points if anyone can further explain what this help file quote above is talking about!

Original Post

I'm trying to get my files more organized as my project grows. So I have a src file now that has all my classes in it. I only have a few but the project will be growing. When I have all my files in the same root folder, along with my assets and .fla file, I'm able to access multiple methods in a class from a separate .as file. I understand that only one method or statement can be declared as public per class file, but that doesn't seem to get in the way of things as long as they are in the same package to begin with. However, when I try to separate them into different packages and folders, I get the error message:

C:\Users\User\Documents\Animation Files\panningTestFolder\panningTest.as, Line 124, Column 28   1195: Attempted access of inaccessible method makeBasicArray through a reference with static type src:LevelSetup.

Line 124: basicArray = levelSetup.makeBasicArray();

To give more detail about my specific setup, I have a class called LevelSetup in a folder called src which is in a folder called Animation Files. Animation Files is the root folder, so to speak, which holds my .fla file.

In other words:

Animation Files>panningTestFolder>panningTest.as is main document class
Animation Files>panningTestFolder>src>LevelSetup.as is the class with methods I need access to (and had access to when they were all in the same folder).

One solution seems to be to put each function that I need into its own class, but sheesh! it seems like it would be easier to just leave all my classes in the same package at that point.

The code I'm using to import the package in my main doc class is:

import src.*;

and the code I'm using to declare the package in the separate .as file is:

package src{

and my goal is to be able to access the functions in that package:

package src{

    import flash.display.*;
    import flash.geom.*;
    import flash.events.*;
    import flash.ui.*;
    import flash.net.URLLoader;

    public class LevelSetup {

        var a:Array = new Array();

        public function LevelSetup() {
            // this is blank
        }

        function makeTile(arr:Array,row,col):int{ // <-- can't access this
            // do stuff
        }

        function makeEmpty2DArray(count:int):Array{ // <-- or this
            // do more stuff
        }

        function makeBasicArray():Array{ // <-- or this from main doc class
            // do other stuff
        }
    }
}

I've gathered that this is because these function are not public and that I can't make more than one be public per class. How do people typically deal with this?

Upvotes: 1

Views: 122

Answers (1)

null
null

Reputation: 5255

I can't make more than one be public per class

That's just wrong. You can have as many public members in a class as you want.

The source of your problem is that you do not specify an access control modifier for the methods makeTile, makeEmpty2DArray and makeBasicArray explicitly. This means they have the default access control modifier internal which means they can only be accessed from within the same package they are defined in. In your case it's the package src.

From your main class, which is likely in a different package (if in any at all) you cannot access those methods. The solution is to make those methods public.

btw: "src" is a horrible package name. Packages are supposed to contain source code, so what information is this name giving you that you don't already have? It's all about organisation similar to files and folders. Would you put all your holiday images into a folder "pictures" on your hard drive? If you want to put all classes related to levels into a package, why not call the package like that: "levels"?

only one package-level declaration per file can use the public attribute, whether that declaration is a class, variable, function, or namespace.

What they mean is that there can be only one public class per package, but this does not restrict the number of public members this class is allowed to have. The members are considered part of the class, not part of the package. This is why it says "package-level"

Upvotes: 1

Related Questions