HTML and CSS Reference
In-Depth Information
pmr.message = "";
pmr.roomId = _room.id;
pmr.zoneId = _room.zoneId;
var esob = new ElectroServer.EsObject();
esob.setFloat("tempX",tempX );
esob.setFloat("tempY",tempY );
esob.setFloat("tempRadius",tempRadius );
esob.setFloat("tempSpeed",tempSpeed );
esob.setFloat("tempAngle",tempAngle );
esob.setFloat("velocityx",tempvelocityx );
esob.setFloat("velocityy",tempvelocityy );
esob.setString("usercolor",usercolor );
esob.setString("ballname",username+ballcounter);
esob.setString("type", "newball");
pmr.esObject = esob;
es.engine.send(pmr);
statusMessages.push("send ball");
}
When a user connected in the same room receives this public message, we handle the
newball event in a similar manner to how we handled the chat text, by using the on
PublicMessageEvent() function. When the function sees an event with the type new
ball , it calls createNetBall() . The createNetBall() function creates ball objects to
bounce around the canvas, much like the ones we created in Chapter 5 :
function onPublicMessageEvent(event) {
statusMessages.push("message received")
var esob = event.esObject;
if (esob.getString("type") == "chatmessage") {
chatMessages.push(event.userName + ":" + esob.getString("message"));
} else if (esob.getString("type") == "newball") {
statusMessages.push("create ball")
createNetBall(esob.getFloat("tempX"),esob.getFloat("tempY"),
esob.getFloat("tempSpeed"),esob.getFloat("tempAngle"),
esob.getFloat("tempRadius"),esob.getFloat("velocityx"),
esob.getFloat("velocityy"),event.userName,esob.getString("usercolor"),
esob.getString("ballname") );
}
}
function createNetBall(tempX,tempY,tempSpeed,tempAngle,tempRadius,tempvelocityx,
tempvelocityy, user, usercolor, ballname) {
tempBall = {x:tempX,y:tempY,radius:tempRadius, speed:tempSpeed, angle:tempAngle,
velocityx:tempvelocityx, velocityy:tempvelocityy,nextx:tempX, nexty:tempY,
mass:tempRadius, usercolor:usercolor, ballname:ballname}
balls.push(tempBall);
}
Figure 11-6 shows what this demo looks like when users click the mouse button to send
balls to other users. The colors of the balls are assigned randomly.
Search WWH ::




Custom Search