The SharedBall sample creates a temporary remote shared object. It’s similar to a multiplayer game. When one user moves the ball, it moves for all other users.

Note:

Use the SharedBall sample files (SharedBall.fla, SharedBall.as, and SharedBall.swf) in the documentation/samples/SharedBall directory in the Adobe Media Server root install directory.

The SharedBall application running in Flash Player

Run the application

  1. Register the application with your server by creating an application directory named SharedBall:

    RootInstall/applications/SharedBall

  2. Open the SharedBall samples files from the documentation/samples/SharedBall directory in the Adobe Media Server root install directory.

  3. Open SharedBall.swf in a web browser.

  4. Open a second instance of SharedBall.swf in a second browser window.

  5. Move the ball in one window and watch it move in the other.

Design the user interface

  1. In Flash, choose File > New > Flash File (ActionScript 3.0) and click OK.

  2. From the toolbox, select the Rectangle tool. Drag to the lower-right corner, then select the Oval tool.

  3. Draw a circle on the Stage. Give it any fill color you like.

  4. Double-click the circle and choose Modify > Convert to Symbol.

  5. In the Convert to Symbol dialog box, name the symbol ball, check that Movie Clip is selected, and click OK.

  6. Select the ball symbol on the Stage and in the Property Inspector (Window > Properties) give it the instance name sharedBall.

  7. Save the file as SharedBall.fla.

Write the client-side code

Be sure to look at the SharedBall.as sample file. These steps present only highlights.

  1. In Flash Professional, create a new ActionScript file.

  2. Create the class, extending MovieClip:

     public class SharedBall extends MovieClip {...}

    The class must extend MovieClip, because the sharedBall symbol in the FLA file is a Movie Clip symbol.

  3. Create the constructor, in which you add event listeners and connect to the server:

         public function SharedBall() 
         {             
             nc = new NetConnection(); 
             addEventListeners(); 
             nc.connect("rtmp://localhost/SharedBall"); 
         }
  4. Add event listeners for netStatus, mouseDown, mouseUp, and mouseMove events:

         private function addEventListeners() { 
             nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
             // sharedBall is defined in the FLA file 
             sharedBall.addEventListener(MouseEvent.MOUSE_DOWN, pickup); 
             sharedBall.addEventListener(MouseEvent.MOUSE_UP, place); 
             sharedBall.addEventListener(MouseEvent.MOUSE_MOVE, moveIt); 
         }
  5. In your netStatus handler, create a remote shared object when a connection is successful. (You’ll also want to create error handlers for rejected and failed connections, shown in the sample AS file.) Connect to the shared object and add a sync event listener:

     switch (event.info.code) 
     { 
         case "NetConnection.Connect.Success": 
             trace("Congratulations! you're connected"); 
                 so = SharedObject.getRemote("ballPosition", nc.uri, false); 
                 so.connect(nc);  
                 so.addEventListener(SyncEvent.SYNC, syncHandler); 
                 break; 
                 ... 
     }
  6. As a user moves the mouse, use setProperty() to set the changing ball location in the remote shared object:

     function moveIt( event:MouseEvent ):void { 
         if( so != null ) 
         { 
         so.setProperty("x", sharedBall.x); 
         so.setProperty("y", sharedBall.y); 
         } 
     }

    When the remote shared object is updated, it dispatches a sync event.

  7. Write a sync event handler that updates all clients with the new ball position:

     private function syncHandler(event:SyncEvent):void { 
         sharedBall.x = so.data.x; 
         sharedBall.y = so.data.y; 
     }

    You can read the value of so.data, even though you can’t write to it.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy