概述
Flex的客户端开发
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import mcTrans.Message;
import mx.messaging.events.MessageAckEvent;
import mx.collections.ArrayCollection;
import mcTrans.TransConst;
import mcTrans.Transceiver;
import mcTrans.TransEvent;
private var times:Date = new Date();
private var username:String;
private var myTrans:Transceiver;
private function initApp():void
{
var path:String = "192.168.1.10"; // "rtmp://192.168.1.10/GameRoom/Game1";
myTrans = new Transceiver(path);
myTrans.addEventListener(TransEvent.State_Event, onStateHeadler)
myTrans.addEventListener(TransEvent.Server_Event, onServerHeadler);
myTrans.addEventListener(TransEvent.UserList_Event, onUserListHeadler);
myTrans.addEventListener(TransEvent.RoomList_Event, onGameRoomListHeadler);
myTrans.addEventListener(TransEvent.MyRoom_Event, onMyRoomHeadler);
myTrans.addEventListener(TransEvent.TalkMsg_Event, onTalkMsgHeadler);
}
private function startConn():void
{
username = txt_username.text;
myTrans.loginHall(username, null);
}
// 服务器返回消息
public function onStateHeadler(evt:TransEvent):void
{
var result:String = evt.EventData.toString();
switch(result)
{
case TransConst.Code_800x30010005:
Alert.show("创建房间失败,重复创建", result);
break;
case TransConst.Code_800x30010012:
Alert.show("不允许重复创建房间", result)
break;
default:
Alert.show("未知错误", result);
break;
}
}
public function onMyRoomHeadler(evt:TransEvent):void
{
var newGameRoomList:Array = evt.EventData as Array;
for(var i:int=0; i<newGameRoomList.length; i++)
{
if (newGameRoomList[i].roomMaster == username)
{
lbl_roomName.text = "房间名:" + newGameRoomList[i].roomName;
lbl_roomId.text = "房间Id:" + newGameRoomList[i].roomId;
}
}
}
// 房间列表
public function onGameRoomListHeadler(evt:TransEvent):void
{
var newGameRoomList:Array = evt.EventData as Array;
if (vb_gameRoomList != null)
vb_gameRoomList.removeAllChildren();
for(var i:int=0; i<newGameRoomList.length; i++)
{
var label:Label= new Label();
label.text = "房间ID:" + newGameRoomList[i].roomId + " 房间名:" + newGameRoomList[i].roomName + " " +
"房主:" + newGameRoomList[i].roomMaster + " 游戏是否开始:" + newGameRoomList[i].gameStart + " " +
"在线用户:" + newGameRoomList[i].personNum + " 上限:" + newGameRoomList[i].personCei;
vb_gameRoomList.addChild(label);
}
}
public function onUserListHeadler(evt:TransEvent):void
{
var newUserList:Array = evt.EventData as Array;
if(vb_userlist != null)
vb_userlist.removeAllChildren();
for(var i:int=0; i<newUserList.length; i++)
{
if ( newUserList[i].ptaccId != username )
{
var label:Label= new Label();
label.text = "房间ID:" + newUserList[i].roomId + " " + newUserList[i].ptaccId + " 的人物";
vb_userlist.addChild(label);
}
else
myLbl.text = "房间ID:" + newUserList[i].roomId + " 你的人物:" + newUserList[i].ptaccId;
}
}
import mx.controls.Alert;
public function onServerHeadler(evt:TransEvent):void
{
var result:String = evt.EventData.toString();
switch(result)
{
case TransConst.Code_800x30010001:
Success();
break;
case TransConst.Code_800x30010005:
Closed();
break;
default:
Alert.show(result, "未知错误");
break;
}
}
private function Success():void
{
btn_stop.enabled = true;
btn_start.enabled = false;
txt_username.enabled = false;
btn_Send.enabled = true;
lbl_title.text = "进入游戏大厅";
}
private function Closed():void
{
btn_stop.enabled = false;
btn_start.enabled = true;
txt_username.enabled = true;
btn_Send.enabled = false;
lbl_title.text = "用户登录";
myLbl.text = "已经退出大厅";
}
private function stopGameHall():void
{
myTrans.netConn.close();
}
// 聊天显示
private function onTalkMsgHeadler(evt:TransEvent):void
{
if (myTrans.soTalk.data.talkMsg != null)
{
var tmp:ArrayCollection = new ArrayCollection();
convertArrayCollection(tmp, myTrans.soTalk.data.talkMsg as ArrayCollection);
for (var i:int = 0; i < tmp.length; i++)
{
var message:Object = tmp.getItemAt(i);
// var name:String = message.nickname;
// var times:String = message.time.toTimeString();
// var msg:String = message.msg;
input_content.text += message.nickname + " " + message.time.toTimeString() +
" 说: " + message.msg + '/n';
}
}
}
// 发送消息
private function sendMsg(nickname:String, msg:String):void
{
var arr:ArrayCollection = new ArrayCollection();
if (myTrans.soTalk.data.talkMsg == null)
arr = new ArrayCollection();
else
convertArrayCollection(arr, myTrans.soTalk.data.talkMsg as ArrayCollection);
var obj:Message = new Message();
obj.nickname = nickname;
obj.msg = msg;
obj.time = new Date();
arr.addItem(obj);
myTrans.sendTalkMsg(arr); // 发送消息
}
private function convertArrayCollection(arrNew:ArrayCollection, arrOld:ArrayCollection):void
{
arrNew.removeAll();
for(var i:int=0; i<arrOld.length; i++)
{
arrNew.addItemAt(arrOld.getItemAt(i), i);
}
}
]]>
</mx:Script>
<mx:Array id="myArr">
<mx:Object label="2"/>
<mx:Object label="4"/>
<mx:Object label="6"/>
<mx:Object label="8"/>
</mx:Array>
<mx:Label x="97" y="12" text="未进入大厅" id="myLbl"/>
<mx:Button x="305" y="10" label="退出大厅" enabled="false" id="btn_stop" click="stopGameHall()"/>
<mx:Label x="8" y="10" text="用户登录" fontWeight="bold" fontSize="12" id="lbl_title"/>
<mx:TextInput x="10" y="38" text="输入你的名字" id="txt_username" width="116" focusIn="txt_username.text=''"/>
<mx:Button x="134" y="38" label="进入大厅" id="btn_start" click="startConn()"/>
<mx:TextInput x="212" y="176" id="input_txt" width="252"/>
<mx:Button x="472" y="176" label="Send" id="btn_Send" enabled="false" click="sendMsg(username, input_txt.text)"/>
<mx:TextArea x="212" y="94" height="74" id="input_content" width="314"/>
<mx:Label x="10" y="68" text="在线列表" fontWeight="bold"/>
<mx:Label x="212" y="68" text="聊天框" fontWeight="bold"/>
<mx:Label x="10" y="219" text="房间信息" fontWeight="bold"/>
<mx:Button x="264" y="217" label="创建房间" click="myTrans.createGameRoom(txt_Room.text, int(cb_Id.value))"/>
<mx:Button x="342" y="217" label="加入房间"/>
<mx:TextInput x="88" y="217" text="酷!酷!酷!" id="txt_Room" width="116" focusIn="txt_username.text=''"/>
<mx:ComboBox id="cb_Id" x="212" y="217" dataProvider="{myArr}"></mx:ComboBox>
<mx:Label id="lbl_roomName" x="10" y="339" text="房间名" fontSize="12" fontWeight="bold"/>
<mx:Label id="lbl_roomId" x="130" y="339" text="房间ID" fontSize="12" fontWeight="bold"/>
<mx:VBox x="10" y="94" id="vb_userlist">
</mx:VBox>
<mx:VBox id="vb_gameRoomList" x="10" y="245" fontSize="12" horizontalScrollPolicy="auto" verticalScrollPolicy="auto">
</mx:VBox>
</mx:Application>
最后
以上就是淡定大白为你收集整理的MMORPG 基础 - (2) 的全部内容,希望文章能够帮你解决MMORPG 基础 - (2) 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复