02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 22: Multi-player games using sockets<br />

69<br />

70 while(pos){<br />

71 socket = (CClientSocket*)m_connectionList.GetNext(pos);<br />

72 if (socket!=pSocket)<br />

73 socket->Send(xmlmsg, strlen(xmlmsg)+1);<br />

74<br />

75<br />

}<br />

76 return;<br />

77<br />

78<br />

}<br />

79 if (strncmp(msg, "DROPBOX", 4)==0){<br />

80 sprintf(xmlmsg, "", pSocket->boxid);<br />

81<br />

82<br />

pSocket->boxid = 0;<br />

83 while(pos){<br />

84 socket = (CClientSocket*)m_connectionList.GetNext(pos);<br />

85 if (socket!=pSocket)<br />

86 socket->Send(xmlmsg, strlen(xmlmsg)+1);<br />

87<br />

88<br />

}<br />

89 return;<br />

90<br />

91<br />

92 }<br />

}<br />

Listing 22.5<br />

Now we have a program that is going <strong>to</strong> handle the connections and pass the data around each<br />

connected user.<br />

Creating the <strong>Flash</strong> application<br />

Open the project ‘Examples\Chapter22\MoveBox.fla’. The code in frame 1 is as follows:<br />

sock = new XMLSocket();<br />

sock.onXML = gotMessage;<br />

sock.onConnect = onSockConnect;<br />

usedids = new Array(0, 0, 0, 0, 0, 0, 0, 0);<br />

dragbox = 0;<br />

sock.connect("192.168.0.1", 5001)<br />

s<strong>to</strong>p ();<br />

The root level variable ‘sock’ is set as a new XML Socket object and the two callback functions for<br />

this object ‘onXML’ and ‘onConnect’ are set <strong>to</strong> ‘gotMessage’ and ‘onSockConnect’ respectively.<br />

The ‘onSockConnect’ function gets called when a connection is made and is passed a Boolean<br />

variable that indicates whether the connection was successfully made. The XMLSocket member<br />

355

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!