
Sample Applications 37
Sample 3: Shared Ball
This sample allows users to move a ball around the screen and to watch while other participants
move the ball around.
About the sample
The previous sample shared text, but you can share graphics as well. This sample allows two users
to share the same ball on the Stage. When a user opens the doc_sharedball.htm file, there is a ball
on the SWF Stage. When any user moves the ball, all other connected users see it move.
Re-creating the sample
The doc_sharedball.fla provides the ActionScript for creating a remote shared object that
synchronizes and updates the ball position for each user.
See “Creating your working environment” on page 31 before you start to re-create the sample.
To create the user interface for this sample:
1 In the Flash MX authoring environment, select File > New to open a new file.
2 From the toolbox, select the Circle tool and draw a circle. With the circle still selected, convert
it to a movie clip by selecting Insert > Convert to Symbol, and name it
ball. In the Property
inspector (Window > Properties), give it the instance name
SharedBall_mc.
3 Create a directory named doc_sharedball in your flashcom application directory, and save the
file as doc_sharedball.fla in this directory.
To write the client-side ActionScript for this sample:
1 Select the keyframe in the Timeline and open the Actions panel (Window > Actions).
2 In the Actions panel, stop the progress of the movie.
stop();
3
Create a new network connection to connect to the server and handle any status messages with
the
onStatus function.
// Create a connection
client_nc = new NetConnection();
// Show connection status in output window
client_nc.onStatus = function(info) {
trace("Level: " + info.level + " Code: " + info.code);
};
// Connect to the application
client_nc.connect("rtmp:/doc_sharedball/room_01");
4
Create a remote shared object to hold the x/y coordinates of the ball.
// Create a remote shared object
ball_so = SharedObject.getRemote("position", client_nc.uri, false);
// Update ball position when another participant moves the ball
ball_so.onSync = function(list) {
SharedBall_mc._x = ball_so.data.x;
SharedBall_mc._y = ball_so.data.y;
};
Kommentare zu diesen Handbüchern