和加载的对象之间进行通信例子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 flashone.as
2
3
4 package {
5 import flash.display.Loader;
6 import flash.display.Sprite;
7 import flash.events.Event;
8 import flash.events.MouseEvent;
9 import flash.events.StatusEvent;
10 import flash.net.LocalConnection;
11 import flash.net.URLRequest;
12 import flash.text.TextField;
13 import flash.text.TextFieldAutoSize;
14 import flash.text.TextFieldType;
15 import flash.display.DisplayObject;
16
17 public class FlashOne extends Sprite {
18 private var conn:LocalConnection;
19
20 private var loadFlashTow:Loader;
21
22 // UI elements
23 private var messageLabel:TextField;
24 private var message:TextField;
25 private var sendBtn:Sprite;
26
27 public function FlashOne() {
28
29 this.loadFlashTow=new Loader();
30 this.loadFlashTow.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
31 this.loadFlashTow.y=100;
32 this.loadFlashTow.x=100;
33 this.loadFlashTow.load(new URLRequest("FlashTow.swf"));
34
35 buildUI();
36 sendBtn.addEventListener(MouseEvent.CLICK, sendMessage);
37 conn = new LocalConnection();
38 conn.addEventListener(StatusEvent.STATUS, onStatus);
39 }
40
41 private function sendMessage(event:MouseEvent):void {
42 conn.send("myConnection", "lcHandler", message.text);
43 }
44
45 private function onStatus(event:StatusEvent):void {
46 switch (event.level) {
47 case "status":
48 trace("LocalConnection.send() succeeded");
49 break;
50 case "error":
51 trace("LocalConnection.send() failed");
52 break;
53 }
54 }
55 private function completeHandler(event:Event){
56 var _content:DisplayObject = event.target.content;
57 this.addChild(loadFlashTow);
58 }
59
60
61 private function buildUI():void {
62 const hPadding:uint = 5;
63 // messageLabel
64 messageLabel = new TextField();
65 messageLabel.x = 10;
66 messageLabel.y = 10;
67 messageLabel.text = "Text to send:";
68 messageLabel.autoSize = TextFieldAutoSize.LEFT;
69 addChild(messageLabel);
70
71 // message
72 message = new TextField();
73 message.x = messageLabel.x + messageLabel.width + hPadding;
74 message.y = 10;
75 message.width = 120;
76 message.height = 20;
77 message.background = true;
78 message.border = true;
79 message.type = TextFieldType.INPUT;
80 addChild(message);
81
82 // sendBtn
83 sendBtn = new Sprite();
84 sendBtn.x = message.x + message.width + hPadding;
85 sendBtn.y = 10;
86 var sendLbl:TextField = new TextField();
87 sendLbl.x = 1 + hPadding;
88 sendLbl.y = 1;
89 sendLbl.selectable = false;
90 sendLbl.autoSize = TextFieldAutoSize.LEFT;
91 sendLbl.text = "Send";
92 sendBtn.addChild(sendLbl);
93 sendBtn.graphics.lineStyle(1);
94 sendBtn.graphics.beginFill(0xcccccc);
95 sendBtn.graphics.drawRoundRect(0, 0, (sendLbl.width + 2 + hPadding + hPadding), (sendLbl.height + 2), 5, 5);
96 sendBtn.graphics.endFill();
97 addChild(sendBtn);
98 }
99 }
100 }
101
102
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 flashtow.as
2
3
4 package {
5 import flash.display.Sprite;
6 import flash.net.LocalConnection;
7 import flash.text.TextField;
8
9 public class FlashTow extends Sprite {
10 private var conn:LocalConnection;
11 private var output:TextField;
12
13 public function FlashTow() {
14 buildUI();
15
16 conn = new LocalConnection();
17 conn.client = this;
18 try {
19 conn.connect("myConnection");
20 } catch (error:ArgumentError) {
21 trace("Can't connect...the connection name is already being used by another SWF");
22 }
23 }
24
25 public function lcHandler(msg:String):void {
26 output.appendText(msg + "\n");
27 }
28
29 private function buildUI():void {
30 output = new TextField();
31 output.background = true;
32 output.border = true;
33 output.wordWrap = true;
34 addChild(output);
35 }
36 }
37 }
38
人和人不要比。自己做自己。