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.

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

In line 17 we create a remote ‘SharedObject’. This is the principal method for maintaining shared<br />

data across a network. The SharedObject resides on the server, but the client can get and set the<br />

data for it. When you create a remote shared object you pass the name of the data, the URI for<br />

the connection object, which is a member variable for the object, and a flag that governs the<br />

persistence for the data. By default, the shared object is not persistent on the client or server; that<br />

is, when all clients close their connections <strong>to</strong> the shared object, it is deleted. To create a shared<br />

object that is saved locally or on the server, you need <strong>to</strong> pass a value for persistence. If the default<br />

is ‘null’ or ‘false’ then no data is saved at either the server or the client. If you pass ‘true’ then the<br />

data is saved on the server. The final option is <strong>to</strong> pass a path. In this case the data is saved on the<br />

client and the server.<br />

1 #include "NetDebug.as"<br />

2<br />

3 avatar = null;<br />

4<br />

5 // Open connection <strong>to</strong> the server<br />

6 client_nc = new NetConnection();<br />

7<br />

8 // Show connection status in output window<br />

9 client_nc.onStatus = function(info) {<br />

10 trace("Level: " + info.level + newline + "Code: " + info.code);<br />

11 };<br />

12<br />

13 // Connect <strong>to</strong> the application<br />

14 client_nc.connect("rtmp:/multiuser/room_01");<br />

15<br />

16 // Create a remote shared object<br />

17 avatars_so = SharedObject.getRemote("avatars", client_nc.uri, false);<br />

18<br />

19 if (avatars_so.data.used1){<br />

20 avatar1.go<strong>to</strong>AndS<strong>to</strong>p(3);<br />

21 }else{<br />

22 avatars_so.data.name1 = "";<br />

23 }<br />

24<br />

...<br />

49 // Update avatar positions when another participant moves<br />

50 avatars_so.onSync = function(list) {<br />

51 avatar1._x = avatars_so.data.x1;<br />

52 avatar1._y = avatars_so.data.y1;<br />

53 avatar1.name = avatars_so.data.name1;<br />

54 avatar2._x = avatars_so.data.x2;<br />

55 avatar2._y = avatars_so.data.y2;<br />

56 avatar2.name = avatars_so.data.name2;<br />

57 avatar3._x = avatars_so.data.x3;<br />

366

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

Saved successfully!

Ooh no, something went wrong!