AS3 Trouble accessing a static class from inside a nested symbol

134 views Asked by At

So basically this is a question about why my calling class (Door) can not identify this static variable class (Game State). it is throwing a #1009 error saying I can not access with GameState.gameState. On my stage I have a Symbol (CampaignLevel_001) that contains additional symbols like the calling symbol (Door) and above that as a separate Symbol (GameState) so essentially I am having trouble getting a nested symbol / class talking to another class.

package game.GameStates
{
    import game.Assets.Player;
    import game.Assets.Turret;
    import game.Assets.Door;
    import flash.events.Event;
    import flash.display.MovieClip;
    import flash.geom.Point;
    import game.Levels.CampaignLevel_001;

public class GameState extends MovieClip
{
        //VARIABLES 
    public var enemyArray : Array;
    public var Bullets : Array = new Array();
    public var Keys : Array = new Array();
    public var HeldKeys : Array = new Array ();
    public var Door : Array = new Array();
    public var Terrain : Array = new Array();
    public var Turrets : Array = new Array ();
    public var Blood : Array = new Array ();
    public var Shields : Array = new Array ();
    public var Levels : Array = new Array ();

    public var NumKeys : int;
    public var DoorLock : Boolean = false;

    public var PlayerSpawnPoint : Point;
    public var CampaignSpawnPoint : Point;

    public static var gameState : GameState;

    public var player : Player;
    public var turret : Turret;

    public var PlayerLives : int = 99;

    public function GameState() 
    {   // constructor code

        gameState = this;
        addEventListener(Event.ADDED_TO_STAGE, Awake);

    }   // constructor code

    private function Awake (e : Event) : void
    {
        enemyArray = new Array();
        trace(enemyArray.length);

    }

    public function OpenDoor () : void
    {
        if (NumKeys <= 0)
        {
            DoorLock = true;
        }
        if (NumKeys > 0)
        {
            DoorLock = false;
        }
    }

    public function NextLevel () : void
    {
        RemoveListeners();

        trace("SHOULD BE GOING TO THE NEXT LEVEL");

        while (Levels.length > 0)
        {
            for each (var level in Levels)
            {
                level.NextLevel ();
            }
        }
    }

    public function RespawnPlayer () : void
    {
        //Spawn Player at player start point

        player = new Player();

        player.x = PlayerSpawnPoint.x;
        player.y = PlayerSpawnPoint.y;

        addChild(player);

        trace("PLAYER ARRAY IS THIS LONG " + enemyArray.length);
        trace("spawned Player!");
    }

    public function setSpellIcons (spell : String , opacity : int) : void
    {
        if (spell == "dodge")
        {
            if (opacity == 0)
            {
                dodgeSymbol.alpha = 0;
            }

            if (opacity == 1)
            {
                dodgeSymbol.alpha = 1;
            }
        }

        if (spell == "shield")
        {
            if (opacity == 0)
            {
                shieldSymbol.alpha = 0;
            }

            if (opacity == 1)
            {
                shieldSymbol.alpha = 1;
            }
        }

    }   //public function setSpellIcons (spell : String , opacity : int) : void


    public function RemoveListeners() : void
    {

        while (enemyArray.length > 0)
        {
            for each  (var enemy : MovieClip in enemyArray)
            {
                enemy.RemovePlayer ();
            }
        }

        while (Door.length > 0)
        {
            for each (var door : MovieClip in Door)
            {
                door.RemoveDoorFunctions ();
            }
        }

        while (Bullets.length > 0)
        {
            for each (var bullet : MovieClip in Bullets)
            {
                bullet.RemoveProjectile();
            }
        }

        while (Keys.length > 0)
        {
            for each (var key : MovieClip in Keys)
            {
                key.RemoveKey ();
            }
        }

        while (Terrain.length > 0)
        {
            for each (var terrain : MovieClip in Terrain)
            {
                terrain.RemoveTerrainListeners();
            }
        }

        while (Turrets.length > 0)
        {
            for each (var turret in Turrets)
            {
                turret.RemoveTurretListeners();
            }
        }

        while (Blood.length > 0)
        {
            for each (var splatter in Blood)
            {
                splatter.RemoveBlood();
            }
        }

        while (Shields.length > 0)
        {
            for each (var shield in Shields)
            {
                shield.RemoveShield();
            }
        }

    }   //public function RemoveListeners() : void


    }
}

The Class that holds (door)

package game.Levels 
{
    import game.GameStates.GameState;
    import flash.display.MovieClip;
    import flash.events.Event;


public class CampaignLevel_001 extends MovieClip 
{
    var currentLevel : int = currentFrame;


    public function CampaignLevel_001() 
    {   // constructor code
        addEventListener(Event.ADDED_TO_STAGE, Awake);
    }

    private function Awake (e : Event) : void
    {
        GameState.gameState.Levels.push(this);
        gotoAndStop(currentLevel);
        trace  ("CURRENT LEVEL IS " + currentLevel);
    }

    public function NextLevel () : void
    {
        var nextLevel : int = currentFrame + 1;
        gotoAndStop(nextLevel);
        trace  ("NEXT LEVEL IS " + nextLevel);
    }
}

}

and the calling class (door) is

package game.Assets 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import game.GameStates.GameState;

public class Door extends MovieClip 
{

    public function Door() 
    { // constructor code
        addEventListener(Event.ADDED_TO_STAGE, Awake);
    }

    private function Awake (e : Event) : void
    {
        GameState.gameState.Door.push(this); 
        GameState.gameState.OpenDoor (); 
        stage.addEventListener(Event.ENTER_FRAME, update); 
        open.alpha = 0; 
    }

    private function update (e : Event) : void
    {
        if (GameState.gameState.DoorLock == true)
        {
            open.alpha = 1;
        }
        if (GameState.gameState.DoorLock == false)
        {
            open.alpha = 0;
        }
    }

    public function RemoveDoorFunctions () : void
    {
        GameState.gameState.Door.splice(GameState.gameState.Door.indexOf(this),1);
        stage.removeEventListener(Event.ENTER_FRAME, update);
        parent.removeChild(this);
    }
}

}

1

There are 1 answers

3
Gone3d On BEST ANSWER

It looks like you're trying to make a Singleton, but haven't added the instance function.

change this:

public static var gameState:GameState;

to:

private static var _gameState:GameState;

and remove the constructor gamestate = this;

and add a function:

public static function get gameState():GameState {
  if{_gameState == null) {
    _gameState = new GameState();
  }
  return _gameState;
}

You can then call it and only get the one instance of GameState by:

GameState.gameState.DoorLock == true;

and any other function in GameState the same way