Hardware Reference
In-Depth Information
As shown in the example, the variables declared at the
beginning of the class are called instance variables . Every
new instance of the class created makes its own copies
of these variables. Every class has a constructor method ,
which gets called to bring the object into existence. You've
already used constructors. When you made a new Serial
port in Processing, you called the constructor method
for the Serial class with something like, myPort = new
Serial(this, portNum, dataRate) .
Here's the constructor for the
Player class. It comes right after the
instance variables in your code. As you
can see, it just takes the values you
give it when you call for a new Player,
and assigns them to variables that
belong to an instance (an individual
player) of the class.
8
public Player (int hpos, int vpos, Client someClient) {
// initialize the local instance variables:
paddleH = hpos;
paddleV = vpos;
client = someClient;
}
Next come the two other methods
mentioned earlier, movePaddle()
and showPaddle() . As you can see,
they use the object's instance variables
( paddleH , paddleV , and client ) to store
the location of the paddle and to draw
it.
8
public void movePaddle(float howMuch) {
float newPosition = paddleH + howMuch;
// constrain the paddle's position to the width of the window:
paddleH = constrain(newPosition, 0, width);
}
public void showPaddle() {
rect(paddleH, paddleV, paddleWidth, paddleHeight);
// display the address of this player near its paddle
textSize(12);
text(client.ip(), paddleH, paddleV - paddleWidth/8 );
}
}
8 This bracket closes the class.
The Main Pong Server Program
Before you write the code for the server as a whole, it's
useful to make a flowchart of what happens. Figure 5-18
shows the main tasks and functions. A few details are
left out for clarity's sake, but what's clear are the main
relationships between the methods that run the program
( setup() , draw() , and serverEvent() ) and the Player
objects. As with any program, the setup() method kicks
things off, and then the draw() method takes over. The
latter sees to it that the screen is updated and listens to
any existing clients. If a new client connects, a server-
Event() message is generated, which causes the method of
that name to run. That method creates new Player objects.
The draw() method takes advantage of the behaviors
inside the Player objects to move and draw their paddles.
That's all the code to define a Player. Put this
code at the end of your program (shown next),
just as if it were another method. To make a
new Player object, write something like, Player newPlayer
= new Player(xPosition, yPosition, thisClient) .
When you do this, the new Player and all its instance
variables and methods are accessible through the variable
called newPlayer (the new Player is not actually stored
in this variable; it's stuffed away in a portion of memory
somewhere that you can get at through the newPlayer
variable). Keep an eye out for this in the program.
 
Search WWH ::




Custom Search