James McGrath
James McGrath

Reputation: 317

AS3: Type was not found or was not a compile-time constant

I'm having an issue with a little app I'm trying to create at the moment, it's my first try and dealing with classes but for some reason I can't create any instances of the class even if it's imported into the document. Here's the code for the class (named "Players"):

package
{
public class Player
{
    public function Player(name_:String)
    {

    }

    public var name_:String;
    private var alignment:int;
    public var healed:Boolean = false;
    public var revealed:Boolean = false;
    public var attacked:Boolean = false;
    public var dead:Boolean = false;

    public function action(target:Player)
    {

    }

    public function describe():String
    {

    }
}

public class Citizen extends Player
{
    public function Citizen(name_:String)
    {
        alignment = 1;
    }

    override public function action(target:Player)
    {

    }

    override public function describe():String
    {
        return "Citizen";
    }
}

public class Investigator extends Player
{
    public function Investigator(name_:String)
    {
        alignment = 1;
    }

    override public function action(target:Player)
    {
        target.revealed = true;
    }

    override public function describe():String
    {
        return "Cop";
    }
}

public class Doctor extends Player
{
    public function Doctor(name_:String)
    {
        alignment = 1;
    }

    override public function action(target:Player)
    {
        target.healed = true;
    }

    override public function describe():String
    {
        return "Doctor";
    }
}

public class Mafioso extends Player
{
    public function Mafioso(name_:String)
    {
        alignment = -1;
    }

    override public function action(target:Player)
    {
        target.attacked = true;
    }

    override public function describe():String
    {
        return "Mafia";
    }
}
}

And the code which creates the instance:

import Players;

stop();

var totalplayers:Number;
var playerArray:Array = new Array();
var playerType:Array = ["Citizen","Cop","Doctor","Mafia"];

var test:Citizen = new Citizen("James");

Both are in the same folder. I get the error code 1046 described in the title but I honestly have no idea why, flash picks it up in the code hints yet it comes up with that! Any help would be appreciated.

Also secondary question, I'll never initiate the Player class (except through inheritance with the other classes), so can I make it private?

Thanks

Upvotes: 0

Views: 1894

Answers (3)

shaunhusain
shaunhusain

Reputation: 19748

Agree with others here should be as shown below (note filenames match class names, file names are denoted in brackets above code blocks). Also you wrote import Players instead of import Player, regardless as the other poster wrote if all classes are currently in the default package the import is unnecessary.

[Player.as]

package
{
    public class Player
    {
        public function Player(name_:String)
        {

        }

        public var name_:String;
        private var alignment:int;
        public var healed:Boolean = false;
        public var revealed:Boolean = false;
        public var attacked:Boolean = false;
        public var dead:Boolean = false;

        public function action(target:Player)
        {

        }

        public function describe():String
        {

        }
    }
}

[Citizen.as]

package
{   
    public class Citizen extends Player
    {
        public function Citizen(name_:String)
        {
            alignment = 1;
        }

        override public function action(target:Player)
        {

        }

        override public function describe():String
        {
            return "Citizen";
        }
    }
}

[Investigator.as]

package
{
    public class Investigator extends Player
    {
        public function Investigator(name_:String)
        {
            alignment = 1;
        }

        override public function action(target:Player)
        {
            target.revealed = true;
        }

        override public function describe():String
        {
            return "Cop";
        }
    }
}

[Doctor.as]

package
{
    public class Doctor extends Player
    {
        public function Doctor(name_:String)
        {
            alignment = 1;
        }

        override public function action(target:Player)
        {
            target.healed = true;
        }

        override public function describe():String
        {
            return "Doctor";
        }
    }
}

[Mafioso.as]

package
{
    public class Mafioso extends Player
    {
        public function Mafioso(name_:String)
        {
            alignment = -1;
        }

        override public function action(target:Player)
        {
            target.attacked = true;
        }

        override public function describe():String
        {
            return "Mafia";
        }
    }
}

It's unfortunate there's no abstract classes as this would be an ideal situation for an abstract class and abstract methods.

Upvotes: 1

Jason Sturges
Jason Sturges

Reputation: 15955

Per your secondary question regarding whether ActionScript supports private classes, if you have a class that would not otherwise be accessed except internally by a public class you may define it as internal.

Internal classes are visible to references inside the current package.

If you do not want a class to be publicly visible outside a package, place the class inside a package and mark the class with the internal attribute. Alternatively, you can omit both the internal and public attributes, and the compiler automatically adds the internal attribute for you. You can also define a class to only be visible inside the source file in which it is defined. Place the class at the bottom of your source file, below the closing curly bracket of the package definition.

In the following example, both X and Y classes are defined in a single file (X.as). X may be referenced and instantiated as normal; however, Y is internal to X and only visible from from the scope of X.

package
{
    import flash.display.Sprite;

    public class X extends Sprite
    {
        public function X()
        {
            super();

            var y:Y = new Y();
        }
    }
}

internal class Y
{
    public function Y()
    {
        trace("internal Y ctor.");
    }
}

This pattern is helpful when a class requires small data models that would not otherwise be accessed outside of a class.

Upvotes: 4

Marty
Marty

Reputation: 39456

I'm assuming all that code is in a file called Players.as.

This is wrong. Each file should contain one class and the class should be the same name as the .as file.

You currently have two classes (Player and Citizen) within one file.

What you need to do is take the Player class you've defined and place it in its own .as. file with the same name (Player). Do the same for Citizen.

Then you can use:

import Player;
import Citizen;

Though this won't be necessary because you don't need to import classes that are in the same directory that you're trying to access it from.

As for the error, you're getting that because Flash is trying to find the class Players and you don't have a class with that name (just a file with that name).

Upvotes: 4

Related Questions