Jump to content

[SOLVED] Shared Objects - Flash Text Chat


Recommended Posts

I got the following action script from a text but am having trouble understanding the details of what the book is instructing.

 

It says to do the following in order to properly implement the script:

 

• Change value of shared object property attribute (for example, text content).

• Trigger SyncEvent.SYNC event due to change in the shared object msg data property.

• Change local property values by user input and assignment to the TextArea component.

• Change remote property values with shared object value.

 

If anyone knows about shared objects and could //comment where these few changes need to be made I would be forever grateful.

 

ActionScript:

package {
import fl.controls.TextArea;
import fl.controls.Button;
import fl.controls.TextInput;
import flash.display.Sprite;
import flash.events.SyncEvent;
import flash.events.NetStatusEvent;
import flash.events.MouseEvent;
import flash.events.FocusEvent;
import flash.net.SharedObject;
import flash.net.NetConnection;
import fl.events.ComponentEvent;
public class TextChat extends Sprite {
	private var button:Button;
	private var text_so:SharedObject;
	private var nc:NetConnection;
	private var textArea:TextArea;
	private var textInput:TextInput;
	private var chatName:TextInput;
	private var rtmpGo:String;
	private var good:Boolean;
	private var catchKey:Boolean;
	private var noName:Boolean;
	public function TextChat() {
		//Set up UIs
		textArea=new TextArea();
		textArea.setSize(500,280);
		textArea.move(20,54);
		addChild(textArea);
		textInput=new TextInput();
		textInput.setSize(500,24);
		textInput.move(20,340);
		textInput.addEventListener(ComponentEvent.ENTER,checkKey);
		addChild(textInput);
		button=new Button();
		button.width=50;
		button.label="Send";
		button.move(20,370);
		button.addEventListener(MouseEvent.CLICK,sendMsg);
		addChild(button);
		chatName=new TextInput;
		chatName.setSize(100,24);
		chatName.move(80, 370);
		chatName.text="<Enter Name>";
		chatName.addEventListener(FocusEvent.FOCUS_IN,cleanName);
		addChild(chatName);
		rtmpGo = "rtmp://192.168.0.11/basicSO";
		nc = new NetConnection( );
		nc.connect(rtmpGo);
		nc.addEventListener(NetStatusEvent.NET_STATUS,doSO);
	}
	private function doSO(e:NetStatusEvent):void {
		good=e.info.code == "NetConnection.Connect.Success";
		if (good) {
			//Set up shared object
			text_so=SharedObject.getRemote("test",nc.uri,false);
			text_so.connect(nc);
			text_so.addEventListener(SyncEvent.SYNC,checkSO);
		}
	}
	private function checkSO(e:SyncEvent):void {
		for (var chng:uint; chng<e.changeList.length; chng++) {
			switch (e.changeList[chng].code) {
				case "clear" :
					break;
				case "success" :
					break;
				case "change" :
					textArea.appendText(text_so.data.msg + "\n");
					break;
			}
		}
	}
	private function cleanName(e:FocusEvent):void {
		chatName.text="";
	}
	private function sendMsg(e:MouseEvent):void {
		noName=(chatName.text=="<Enter Name>" || chatName.text=="");
		if (noName) {
			textArea.appendText("You must enter your name \n");
		} else {
			text_so.setProperty("msg",chatName.text +": "+ textInput.text);
			textArea.appendText(chatName.text +": "+textInput.text + "\n");
			textInput.text="";
		}
	}
	private function checkKey(e:ComponentEvent):void {
		noName=(chatName.text=="<Enter Name>" || chatName.text=="");
		if (noName) {
			textArea.appendText("You must enter your name \n");
		} else {
			text_so.setProperty("msg",chatName.text +": "+ textInput.text);
			textArea.appendText(chatName.text +": "+textInput.text + "\n");
			textInput.text="";
		}
	}
}
}

Link to comment
https://forums.phpfreaks.com/topic/127212-solved-shared-objects-flash-text-chat/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.