• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Participant ,
May 13, 2011 May 13, 2011

Copy link to clipboard

Copied

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

}

TOPICS
ActionScript

Views

485

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , May 13, 2011 May 13, 2011

  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.

Votes

Translate

Translate
Explorer ,
May 13, 2011 May 13, 2011

Copy link to clipboard

Copied

  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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 13, 2011 May 13, 2011

Copy link to clipboard

Copied

LATEST

Perfect!

Many thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines