1

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);
    }
}

}

KingWrex
  • 13
  • 3
  • You probably haven't initialized the static var anywhere. #1009 means reference is null. – Vesper Nov 11 '14 at 06:09
  • 1
    you put in the constructor GameState "gameState = this" but when you refer to GameState.gameState you don't pass by constructor (because is a static variable). So your variabile is NULL and when yo try to refer to properties of your variable you have the dexcribed error (1009) – Joe Taras Nov 11 '14 at 09:29
  • what might you recommend doing? I attempted putting gameState = this into the awake function but it still does not work. Will this not work as a static variable? If you dont mind, how might I be able to access it? – KingWrex Nov 13 '14 at 04:47

1 Answers1

0

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

Gone3d
  • 1,189
  • 1
  • 8
  • 20
  • 1
    That appears to have worked! I just need to adjust my other classes and functions to make it functional. Thankyou – KingWrex Nov 14 '14 at 01:06
  • Actually I am still getting a problem, when the gameState attempts to use the function RespawnPlayer() the player that is spawned can not access its ADDED_TO_STAGE event in its constructor public function Player() { addEventListener(Event.ADDED_TO_STAGE, Awake); } private function Awake (e : Event) : void { trace("spawned Player!"); GameState.gameState.enemyArray.push(this); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown); stage.addEventListener(KeyboardEvent.KEY_UP, keyUp); stage.addEventListener(Event.ENTER_FRAME, update); } – KingWrex Nov 14 '14 at 05:02
  • You should probably look at changing the GameState class to not exend MovieClip. Have another main level class handle putting things on the stage (addChild), that way you can manage things in the GameState, but handle the stage elsewhere since it's a static var – Gone3d Nov 14 '14 at 14:11