AS3无法从嵌套符号内访问静态类

时间:2014-11-11 03:48:02

标签: actionscript-3 static-class

所以基本上这是一个关于为什么我的调用类(Door)无法识别这个静态变量类(Game State)的问题。它抛出#1009错误,说我无法使用GameState.gameState访问。在我的舞台上,我有一个符号(CampaignLevel_001),其中包含其他符号,如调用符号(Door)及其上方,作为单独的符号(GameState),所以基本上我无法让嵌套符号/类与另一个类交谈。

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


    }
}

持有的门(门)

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

}

并且调用类(门)是

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 个答案:

答案 0 :(得分:0)

看起来你正在尝试创建一个Singleton,但还没有添加实例函数。

改变这个:

public static var gameState:GameState;

为:

private static var _gameState:GameState;

并删除构造函数gamestate = this;

并添加一个函数:

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

然后你可以调用它,只获得GameState的一个实例:

GameState.gameState.DoorLock == true;

和GameState中任何其他函数的方式相同