Hardware Reference
In-Depth Information
When new clients connect to the
server, the net library's server-
Event() method is called automatically;
your Processing sketch has to have
this method in order to respond to the
event. It uses the new client to create
a new Player object using a method
called makeNewPlayer() . At right is the
serverEvent() method.
8
// The ServerEvent message is generated when a new client
// connects to the server.
void serverEvent(Server thisServer, Client thisClient) {
if (thisClient != null) {
// iterate over the playerList:
for (int p = 0; p < playerList.size(); p++) {
// get the next object in the ArrayList and convert it
// to a Player:
Player newPlayer = (Player)playerList.get(p);
// if thisPlayer's client matches the one that generated
// the serverEvent, then this client is already a player, so quit
// out of the method and return:
if (newPlayer.client == thisClient) {
return;
}
}
// if the client isn't already a Player, then make a new Player
// and add it to the playerList:
makeNewPlayer(thisClient);
}
}
8
Now that you've seen the draw()
and the serverEvent() methods,
it's time to look at the methods
they call. It's best to start with the
creation of a new Player, so here's the
makeNewPlayer() method.
void makeNewPlayer(Client thisClient) {
// paddle position for the new Player:
int x = width/2;
// if there are no players, add to the top:
int y = nextTopPaddleV;
/*
Get the paddle position of the last player on the list.
If it's on top, add the new player on the bottom, and vice versa.
If there are no other players, add the new player on the top.
*/
// get the size of the list:
int listSize = playerList.size() - 1;
// if there are any other players:
if (listSize >= 0) {
// get the last player on the list:
Player lastPlayerAdded = (Player)playerList.get(listSize);
// is the last player's on the top, add to the bottom:
if (lastPlayerAdded.paddleV == nextTopPaddleV) {
nextBottomPaddleV = nextBottomPaddleV - paddleHeight * 2;
y = nextBottomPaddleV;
}
// is the last player's on the bottom, add to the top:
else if (lastPlayerAdded.paddleV == nextBottomPaddleV) {
A new Player is added to the bottom
team if the last player is on the top, and
vice versa.
The variables nextTopPaddleV and
nextBottomPaddleV keep track of
the positions for the next players on
each team.
ยป
 
Search WWH ::




Custom Search