O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Socket to Load Display Assets at Runtime

At the beginning of this chapter, we learned that ActionScript provides two different mechanisms for adding an external display asset to an application at runtime:

  • The flash.display.Loader class

  • The flash.net.Socket class, used in combination with the Loader class's instance method loadBytes( )

Now that we're comfortable with the Loader class, let's examine how it can be combined with the Socket class to retrieve display assets over a raw TCP/IP socket. The technique described in this section might be used when loading assets for a socket-based application such as a multiuser game, or simply to prevent the loaded asset from appearing in the end user's cache.

Here's the general process for retrieving display assets over a raw TCP/IP socket:

  1. Connect to a server that can transfer a GIF, PNG, JPEG, or SWF file in binary format to the Flash runtime.

  2. Retrieve the bytes for the desired asset.

  3. Convert the loaded bytes to an asset object, suitable for on-screen display.

To accomplish the first two of the preceding steps, we use ActionScript's flash.net.Socket class. The Socket class communicates with a server in binary data format (in raw bytes). To accomplish the final step, we use the Loader class's instance method loadBytes( ) method. The loadBytes( ) method converts raw bytes to an ActionScript display object.

The following sections describe the preceding process in detail.

Server-Side: Sending the Asset

Using the Socket class, we can retrieve the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required