Game Development Reference
In-Depth Information
Sending the data is as simple as calling the send() method on the WebSocket object. The WebSocket's
readyState property has the value 1 when it is open and functioning correctly. With those changes, we're
totally online-enabled! One last tweak to make is to draw player names above their cars. The DrawGame
method is shown in Listing 9-30.
Listing 9-30. Modification of DrawGame Method in the Client
function DrawGame()
{
// Clear the screen
GraphicsContext.clearRect(0, 0, GP.GameWidth, GP.GameHeight);
GraphicsContext.save();
GraphicsContext.font = "12pt Arial";
GraphicsContext.fillStyle = "black";
GraphicsContext.textAlign = "center";
for (var i = 0; i < Cars.length; i++)
{
GraphicsContext.save();
GraphicsContext.translate(Cars[i].X | 0, Cars[i].Y | 0);
GraphicsContext.rotate(Cars[i].OR);
GraphicsContext.drawImage(CarImage, -CarImage.width / 2 | 0,
-CarImage.height / 2 | 0);
GraphicsContext.restore();
if (Cars[i].Name)
{
GraphicsContext.fillText(
(Cars[i] == MyCar ? "Me" : Cars[i].Name.substring(0, 10)),
Cars[i].X | 0,
(Cars[i].Y - GP.CarRadius - 12) | 0
);
}
}
GraphicsContext.restore();
}
And we are ready to play. Invite all your friends!
 
Search WWH ::




Custom Search