- Local Connection
Download: BallMotionSenderReceiver.zip
兩個swf要互相通訊可以藉由LocalConnection來完成,不需要透過第三方的Communication Server來溝通。範例畫面如下:
Sender:
只有兩個按鈕,負責傳送控制訊息到Reciver端。
Receiver:
接收端上只是一個停滯的球體,等待傳送端發送啟動訊息。
主要程式片段如下:
/* in LocalConnSend.as */
public function LocalConnSend(){
initUI();
conn = new LocalConnection();
conn.addEventListener(StatusEvent.STATUS, onStatus);
}
private function sendMessage(event:MouseEvent):void {
conn.send("myConnection", "lcHandler", event.target.parent.getLabel());
trace(event.target.parent.getLabel());
}
/* in LocalConnRecv.as */
public function lcHandler(msg:String):void {
switch(msg){
case "Run":
mBall.run();
break;
case "Stop":
mBall.stop();
break;
default:
trace(msg + "\n");
break;
}
}
對應到sender裡的lcHandler,依造所接收的字串給予對應的動作。
其於詳細部分請參考原始碼。
Thiis was a lovely blog post
ReplyDelete