Skip navigation
Currently Being Moderated

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

May 13, 2011 2:13 AM

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);
                }
            }
        }
    }
 
} 
 
 
 
 
Replies
  • Currently Being Moderated
    May 13, 2011 6:33 AM   in reply to jfweber

      If you are using Flash IDE, search in Library for symbols that has defined Class property as "Player" or "Table" and change to something different, for example "_Player" and "_Table". Setting deletePlayer() to protected may cause RTE.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points