12

Can I put two or more actionscript classes in one .as file like this:

//A.as
package classes {

    public class A {
        public function A() {
            var b:B = new B();
        }
    }
    internal class B {
        public function B() {
            trace("Hello");
        }
    }
}

It doesn't work in Flash Builder:

A file found in a source-path can not have more than one externally visible definition. classes:A; classes:B

If it possible, I'm going to ask next question.
Can I place two or more packages with multiple classes in one .as file?

someOne
  • 1,975
  • 2
  • 14
  • 20
user578737
  • 133
  • 1
  • 8

1 Answers1

26

No and no. The following works:

//A.as

package classes {

    public class A {
        public function A() {
            var b:B = new B();
        }
    }

}
class B { // <--- Note the class is outside of the package definition.
    public function B() {
        trace("Hello");
    }
}

The class B is only visible to the class A - you cannot have more than one visible class in one file (exactly what the error message states). And you cannot have more than one package in a file.

someOne
  • 1,975
  • 2
  • 14
  • 20
Tomasz Stanczak
  • 12,796
  • 1
  • 30
  • 32