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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

The example we are going <strong>to</strong> study in this chapter is deliberately kept very simple. There is a lot<br />

<strong>to</strong> learn when understanding sockets and so your time is better spent understanding the principles<br />

rather than getting bogged down in the intricacies of the game design. In the example there are<br />

up <strong>to</strong> eight boxes. A user can click and drag any unused box. When a box is being dragged, it<br />

will be seen moving on the screen of every other computer connected <strong>to</strong> the site. If a user has<br />

entered their name in the box at the <strong>to</strong>p, then their name will be displayed in the box. Very simple<br />

maybe, but it illustrates the concepts of multiple users receiving and passing data in real-time across<br />

a remote network. Before we can create the <strong>Flash</strong> application we need a program <strong>to</strong> be running<br />

on the server. This program acts as the conduc<strong>to</strong>r for all the connected computers. We are going<br />

<strong>to</strong> create this program using Visual C++.<br />

Sockets on a Windows server<br />

This section is going <strong>to</strong> use C++ code. The syntax<br />

for C++ is very similar <strong>to</strong> the JavaScript syntax you<br />

are familiar with from using <strong>Flash</strong> <strong>ActionScript</strong>. We<br />

are going <strong>to</strong> use something called Microsoft Foundation<br />

Classes. This is a library of C++ classes (another word<br />

for objects) that make programming Windows applications<br />

easier and more robust. To make use of the example<br />

on the CD, you will need a copy of Visual C++ on<br />

a PC. If you can, open the file ‘Examples\Chapter22\<br />

SockListener\SockListener.dsw’. This file is a Visual C++<br />

project file. A C++ project uses several files <strong>to</strong> s<strong>to</strong>re the<br />

source code and other information required by the IDE<br />

(Integrated Development Environment) <strong>to</strong> compile the<br />

program. The source we will examine is in the file ‘Sock-<br />

ListenerDlg.cpp’, which is simply a text file; so if you don’t<br />

have Visual C++ then just open this in a text viewer. MFC<br />

applications come in several flavours; this one uses a dialog<br />

box as the main window. Figure 22.2 shows the applica-<br />

Figure 22.2 The SockListener<br />

application<br />

tion running. Visually the program is just a list box where we can add text information that makes<br />

understanding the program’s operation easier. The purpose of the program is <strong>to</strong> open a server<br />

socket on a defined port on a computer with a static IP address and listen for users across the<br />

Internet who want <strong>to</strong> connect <strong>to</strong> this socket. Whenever there is a new connection it is s<strong>to</strong>red in a<br />

list and the program sees <strong>to</strong> it that all the current listeners <strong>to</strong> this socket get fed with information.<br />

The example program allows remote users <strong>to</strong> move boxes on another user’s computer and vice<br />

versa.<br />

Initializing the dialog box<br />

Whenever a new dialog box is created, MFC calls the function ‘OnInitDialog’ in the dialog box<br />

class. Most of this is standard <strong>to</strong> all dialog boxes; a menu is created and the icon for the application<br />

348

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

Saved successfully!

Ooh no, something went wrong!