【问题标题】:AS3 Trouble accessing a static class from inside a nested symbolAS3 无法从嵌套符号内部访问静态类
【发布时间】:2014-11-11 03:48:01
【问题描述】:

所以基本上这是一个关于为什么我的调用类(门)无法识别这个静态变量类(游戏状态)的问题。它抛出 #1009 错误,说我无法使用 GameState.gameState 访问。在我的舞台上,我有一个符号(CampaignLevel_001),其中包含额外的符号,如调用符号(门)和上面的一个单独的符号(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);
    }
}

}

【问题讨论】:

  • 您可能还没有在任何地方初始化静态变量。 #1009 表示引用为空。
  • 您放入了构造函数 GameState "gameState = this" 但是当您引用 GameState.gameState 时,您不会通过构造函数传递(因为是静态变量)。因此,您的变量为 NULL,当您尝试引用变量的属性时,您会遇到错误描述 (1009)
  • 您建议做什么?我尝试将 gameState = this 放入唤醒功能,但它仍然不起作用。这不能作为静态变量工作吗?如果你不介意,我怎么能访问它?

标签: actionscript-3 static-class


【解决方案1】:

您似乎正在尝试制作单例,但尚未添加实例函数。

改变这个:

public static var gameState:GameState;

到:

私有静态变量 _gameState:GameState;

并移除构造函数gamestate = this;

并添加一个函数:

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

然后您可以调用它并通过以下方式仅获取一个 GameState 实例:

GameState.gameState.DoorLock == true;

和 GameState 中的任何其他函数一样

【讨论】:

  • 这似乎奏效了!我只需要调整我的其他类和函数以使其正常工作。谢谢你
  • 其实我还是遇到了一个问题,当 gameState 尝试使用函数 RespawnPlayer() 时,生成的玩家无法在其构造函数中访问其 ADDED_TO_STAGE 事件 public function Player() { addEventListener(Event .ADDED_TO_STAGE,醒着); } 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); }
  • 您可能应该考虑将 GameState 类更改为不扩展 MovieClip。让另一个主级别类处理将东西放在舞台上(addChild),这样你就可以在 GameState 中管理东西,但在其他地方处理舞台,因为它是一个静态变量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 2011-03-29
  • 2021-04-28
  • 1970-01-01
  • 2010-09-09
相关资源
最近更新 更多