2 Replies Latest reply: May 13, 2011 6:59 AM by jfweber RSS

    Access Modifiers - strange behavior (public does not work but protected does) - why?

    jfweber Community Member

      I encountered some unexpected behavior for theaccess modifiers I cannot explain; maybe someone can help me out:

       

      There are two classes:

       

      "Player" und "Table" - both inherit from the same class and are located in the same package in the same directory.

      "Player" wants to call function "deletePlayer" in "Table" but this seems to only work when I have the access modifier set to "protected" - if the function "deletePlayer" is public I receive an error:

       

      Player.as, Line 34     1118: Implicit coercion of a value with static type modules.room:Player to a possibly unrelated type Player.

       

       

       

      Why does public not work?

       

      public class Player extends UIObject
          {
              
              private var table                    : Table;
              
              
              public function Player() 
              {
                  
              }
              
              public override function init($table : MovieClip) : void
              {
                  table = Table($table);
              }
              
              
              public function deletePlayer() : void
              {
                  
                  table.deltePlayer(this);       // This calls the function in Table
                  delete this;
              }
              
          }
      
      package modules.room 
      {
          
          import flash.display.MovieClip;
          import flash.events.MouseEvent;
          import modules.room.Room;
          import modules.room.Player;
          import modules.UIObject;
           
          
          public class Table extends UIObject
          {
              
              private var players             : Array = new Array();
              private var timer                : Timer;
              private var room                : Room;        
              
              public function Table() 
              {
                  
              }
              
              
              public override function init($room : MovieClip) : void
              {
                  trace("init Table" + $room);
                  room = Room($room);
                  
                  initBtnCreateTable();
              }
              
              
              private function initBtnCreateTable() : void
              {
                  btnCreatePlayer.addEventListener(MouseEvent.CLICK, createPlayer);
              }
              
              
              public function createPlayer($event : MouseEvent) : Player
              {
                  trace("creating player");
                  var player : Player = Player(addChild(new Player()));
                  players.push(player);
                  player.name = "Player" + players[players.length - 1];
                  player.init(this);
                  
                  return player;
              }
              
          
              public function deltePlayer($player : Player) : void          // This will throw an error until I set access modifier to protected
              {
                  for (var i : int = 0; i < players.length; i++)
                  {
                      if (players[i] == $player)
                      {
                          players.splice(i, 1);
                          trace(players);
                      }
                  }
              }
          }
      
      }