flash oop_playermanager

The PlayerManager class

Object programming makes complete sense when we need multiple objects to work together. As a simple task we are now going to create a class which will add or delete our players from the application, other functionalities such as sorting will be added later.

The goal of the playerManager class is to centralise the management of players in our application. Next to our player class we define a new class called player manager:

package
{
public class PlayerManager
{
public function PlayerManager ( )
{
}
}
}

We define an instance property arrayPlayers in order to contain each player:

package
{
public class PlayerManager
{
// array containing player references
private var arrayPlayers:Array = new Array();
public function PlayerManager ( )
{
}
}
}

The addPlayer method adds the reference to the player passed in as a parameter to the internal array arrayPlayers and calls the present method on each player entering:

package
{
public class PlayerManager
{
// array containing player references
private var arrayPlayers:Array = new Array();
public function PlayerManager ( )
{
}
public function addPlayer ( pPlayer:Player ):void
{
pPlayer.present();
arrayPlayers.push ( pPlayer );
}
}
}

The addPlayer method accepts a player type parameter, only the player type objects could therefore be passed. When a player is added the present method is called on it. To add players we create each instance then add them to the player manager, the PlayerManager class:

// instancing of a player
var myPlayer:Player = new Player ("Stevie", "Wonder", 57, "Michigan");
var mySecondPlayer:Player = new Player ("Bobby", "Womack", 66, "Detroit");
var myThirdPlayer:Player = new Player ("Michael", "Jackson", 48, "Los
Angeles");
// management of Players
var myManager:PlayerManager = new PlayerManager();
// addition of players
// displays :
/*
My name is Stevie, I am 57 years old.
My name is Bobby, I am 66 years old.
My name is Michael, I am 48 years old.
*/
myManager.addPlayer ( myPlayer );
myManager.addPlayer ( mySecondPlayer );
myManager.addPlayer ( myThirdPlayer );

When a player is added, it is automatically presented. In the case where a player is deleted by the system, we have to delete it from the manager, we define a deletePlayer method. This searches for the player in the internal array thanks to the indexOf method and deletes it if it is found. Otherwise, we display a message indicating that the player is no longer present in the manager:

package
{
public class PlayerManager
{
// array containing player references
private var arrayPlayers:Array = new Array();
public function PlayerManager ( )
{
}
public function addPlayer ( pPlayer:Player ):void
{
pPlayer.present();
arrayPlayers.push ( pPlayer );
}
public function deletePlayer ( pPlayer:Player ):void
{
var positionPlayer:int = arrayPlayers.indexOf ( pPlayer );
if ( positionPlayer != -1 ) arrayPlayers.splice (
positionPlayer, 1 );
else trace("Player not present!");
}
}
}

We can delete a player by passing its reference. In order to externalize the array containing all of the players, we define a getPlayers access method:

package
{
public class PlayerManager
{
// array containing player references
private var arrayPlayers:Array;
public function PlayerManager ( )
{
arrayPlayers = new Array();
}
public function addPlayer ( pPlayer:Player ):void
{
pPlayer.present();
arrayPlayers.push ( pPlayer );
}
public function deletePlayer ( pPlayer:Player ):void
{
var positionPlayer:int = arrayPlayers.indexOf ( pPlayer );
if ( positionPlayer != -1 ) arrayPlayers.splice (
positionPlayer, 1 );
else trace("Player not present!");
}
public function getPlayers ( ):Array
{
return arrayPlayers;
}
}
}

In order to get the group of players we call the getPlayers method on the instance of the PlayerManager class:

// instancing of a player
var myPlayer:Player = new Player ("Stevie", "Wonder", 57, "Michigan");
var mySecondPlayer:Player = new Player ("Bobby", "Womack", 66, "Detroit");
var myThirdPlayer:Player = new Player ("Michael", "Jackson", 48, "Los
Angeles");
// management of Players
var myManager:PlayerManager = new PlayerManager();
// addition of players
myManager.addPlayer ( myPlayer );
myManager.addPlayer ( mySecondPlayer );
myManager.addPlayer ( myThirdPlayer );
// recuperation of the group of players
// displays : [Player name : Stevie, surname : Wonder, age : 57, town :
Michigan],[Player name : Bobby, surname : Womack, age : 66, town :
Detroit],[Player name : Michael, surname : Jackson, age : 48, town : Los
Angeles]
trace( myManager.getPlayers() );

In order to delete the players we call the deletePlayer method by making the player delete the reference:

instancing of a player
var myPlayer:Player = new Player ("Stevie", "Wonder", 57, "Michigan");
var mySecondPlayer:Player = new Player ("Bobby", "Womack", 66, "Detroit");
var myThirdPlayer:Player = new Player ("Michael", "Jackson", 48, "Los
Angeles");
// management of Players
var myManager:PlayerManager = new PlayerManager();
// addition of players
myManager.addPlayer ( myPlayer );
myManager.addPlayer ( mySecondPlayer );
myManager.addPlayer ( myThirdPlayer );
// deletion of two players
myManager.deletePlayer ( myPlayer );
myManager.deletePlayer ( myThirdPlayer );
// recuperation of the group of players
// displays : [Player name : Bobby, surname : Womack, age : 66, town :
Detroit]
trace( myManager.getPlayers() );

The PlayerManager class allows us to manage the different players created, we would add numerous functionalities. In object oriented programming there is no single and unique way to conceive each application. It is exactly this that is the richness of object oriented development. We can discuss for hours the way in which we have developed an application, some will agree and others will not. It is just good to share ideas.

When we develop an application we are often confronted with problems that another developer has surely encountered before us. To bring concrete solutions to recurring problems we can use design patterns. These define the way in which we can separate and design our application still maintaining the goal of optimization, portability and reusability. We will soon revisit the most used design patterns.

Our Player and PlayerManager classes are from now on used in our applications, but the client wants to add the concept of moderators. By thinking for a few moments we can see that a moderator is a sort of player, but with specific rights such as being able to exclude another player or to stop a part in progress. An administrator therefore has all the capacities of a player. It will be redundant in this case to redefine all the methods and properties already defined in the player class in the administrator class, to reuse our code we will use inheritance.








Adobe Training center


Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42