More on Nonpersistent Client-Side Remote Shared Objects
(Page 1 of 4 )
Last week, we introduced you to some of the advantages of using remote shared objects in your applications. This week, in the conclusion to this two-part article, we'll apply what we learned with a few basic projects. This article is excerpted from chapter four of
Learning Flash Media Server 2, an O'Reilly PDF Book. Copyright © 2008 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available direct from O'Reilly Media.
Minimalist Project for Shared Text
Having seen a minimalist project for a movie clip, we’ll now look at one for sharing text. The concepts are essentially the same, but instead of basing the changes in the shared object slots on the movement of the mouse position on the stage, the changes are based on typed input by the user.
In this application, the user changes the value of the shared object property attribute (text content) by typing in the value and pressing a button to fire a function. The function enters the value of the text input field and assigns that value to the shared object attribute. Here are the basic elements of the project.
Classes
NetConnection
SharedObject
Objects
Dynamic Text Field (1)
Input Text field (1)
Button Component (1)
Follow these steps to create the application:
In the server-side FMS2 root, create a new folder and name it zbasicSO. (This is the same server-side folder used in all of the applications in this chapter, and if you have it in place already, you don’t have to add another one.)
Open a new Flash document and set up the following layers, from top to bottom: Actions, Text Fields, Button, and Label. Lock the layers.
- Unlock the Text Fields layer and add a large, multiline dynamic text field, and below it add a single line input text field. Use Figure 4-3 as a guide. In the Properties panel, enter the instance namechat_txtfor the large text field and message_txtfor the small text field. Check the “Show border around text” property for both text fields. Lock the layer.
- Unlock the Button layer and add a button beneath the smaller of the two text fields. Provide the button with the instance namesend_button. It will be used to trigger the change in the shared object. Lock the layer.
- In the Label layer, use static text to add a label across the top of the application. Figure 4-3 shows the label used in this example.

Figure 4-3. Text fields, and instance names
- Lock all the layers, click on the first frame of the Actions layer, and add the following script:
1 //Label button components
2 send_button.label = "Send";
3 var rtmpGo:String;
4 var text_so:SharedObject;
5 rtmpGo="rtmp:/zbasicSO"
6 //rtmpGo = "rtmp://domain.name.com/zbasicSO";
7 var nc:NetConnection = new NetConnection();
8 nc.connect(rtmpGo);
9 //Set up shared object
10 text_so = SharedObject.getRemote("sharedtext", nc.uri, false);
11 text_so.connect(nc);
12 text_so.onSync = function() {
13 if (text_so.data.textValue != undefined) {
14 _level0.chat_txt.text += text_so.data.textValue;
15 }
16 };
17 //Send remote shared object
18 var sender:Object = new Object(); 19 sender.click = function() {
20 text_so.data.textValue = message_txt.text+newline;
21 message_txt.text = "";
22 };
23 send_button.addEventListener("click", sender);
When you test this application, you will need to run two copies of it. Even though it’s a minimum application, it’s perfectly functional and you could chat with others all day long with this little app. Just remember this sequence when coding chat
applications:
Change value of shared object property attribute (e.g., text content)
TriggerSharedObject.onSyncfunction
Change local properties with shared object value within the function
When you look at client-side shared objects as a simple sequence, it’s not so hard to see how they are set up in a script.
Next: Minimalist Project for Shared Function >>
More Flash Articles
More By O'Reilly Media