论坛首页 编程语言技术论坛

flex游戏多人为实现

浏览 3976 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-05-07  
借用了一下blazeds 广播信息produce consumer ,大致意思就是,服务器广播给每个客户端一个名字,和目标坐标

具体的人物移动,由客户端来完成,不必要每移动一点就广播一次,有点乱

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="" xmlns:local="*">

	<mx:RemoteObject id="saveuser" destination="Allroles" endpoint="http://192.168.200.14:8400/roleOnWeb/messagebroker/amf">

		

	</mx:RemoteObject>

	<mx:RemoteObject id="updateRole" destination="Allroles"  endpoint="http://192.168.200.14:8400/roleOnWeb/messagebroker/amf">

		

	</mx:RemoteObject>

	<mx:Script>

		<![CDATA[

			import mx.controls.Alert;

			import simple.MyEvent;

			import mx.events.ChildExistenceChangedEvent;

			import mx.collections.ArrayCollection;

			import entity.Role;

			import mx.rpc.events.ResultEvent;

			import simple.People;

			import mx.managers.PopUpManager;

			import mx.controls.Alert;

			import simple.UtilPeopleRole;

			

			

			public var firstName:String;

			

			private var people:People;

			

			[Bindable]

			public var allRole:ArrayCollection=new ArrayCollection();

			

			public var allPeople:ArrayCollection=new ArrayCollection();

			

			

			/**

			 * 初始化,初始化producer,consumer

			 * 

			 * **/

			public function initApp(){

				trace("initApp");

				initChannel(producer,consumer);

				initChannel(sendPoint,recivePoint);

				consumer.subscribe();

				recivePoint.subscribe();

				initPeople();

				init.enabled=false;

				

			}

			/**

			 * 初始化people

			 * **/

			public function initPeople():void{

				var fx:Number=parseInt(dx.text)

				var fy:Number=parseInt(dy.text);

				var people=UtilPeopleRole.getPeople(fx,fy,fname.text);

				//让服务器保存这个people;

				saveRole(people);

				trace("peple.name"+people.name)

			}

			

			/**

			 * 让server保存这个People

			 * **/

			public function saveRole(p:People):void{

				//添加一个事件监听,添加本role,并把当前添加后返回的array得到遍历他,

				//初始化其他角色

				saveuser.addEventListener(ResultEvent.RESULT,resultArr);

				

				//通过people相应的数据获得一个role对象

				var role:Role=UtilPeopleRole.peopleToRole(p);

				

				firstName=p.name;

				

//				mx.controls.Alert(role.name+"pname:"+p.name);

				//服务器添加这个Role,并返回添加后的数组;

				saveuser.addRole(role);

				/**向所有人广播,谁进来了**/

				sendMessage(role);

			}

			/**

			 * 从服务器得到数据,然后显示所有在线人

			 * 

			 * **/

			public function resultArr(e:ResultEvent):void{

				

				if(e.result is ArrayCollection){

					allRole=e.result as ArrayCollection

					for each(var role:Role in allRole){

						addPeople(role);//向本数组中添加people

						trace(role.name);

					}

				}

			}

			/**

			 * 为本应用添加其他人,向本应用添加所有 服务器返回的Role

			 * **/

			public function addPeople(role:Role):void{

				//通过role信息返回people信息;

				var people:People=UtilPeopleRole.RoleToPeople(role.x,role.y,role.name);

				

				trace("role.name:+"+role.name+"p:"+people.name+"f:"+firstName);

				if(people.name==firstName){

					Application.application.addEventListener(MouseEvent.CLICK,people.moves);

				}

				//添加People本程序添加

				addChild(people);

				//数组中添加

				allPeople.addItem(people);

				//其他人移动时添加一个事件,

				people.addEventListener(MyEvent.MOVING,otherMove);

			}

			

		]]>

	</mx:Script>

	<mx:Producer id="sendPoint" destination="move">

		

	</mx:Producer>

	

	<mx:Consumer id="recivePoint" destination="move" message="getPointMessageHandle(event)">

		

	</mx:Consumer>

	<mx:Script>

		<![CDATA[

			import mx.messaging.MessageAgent;

			public function otherMove(e:MyEvent):void{

				trace("buhuoshijian");

				

				//这是角色走动时的消息驱动

//				initChannel(sendPoint,recivePoint);

				

				

//				if(e.target is People){

					var p:People=e.target as People;

					//通知所有客户端

					sendPointMessage(p,e);

					

//				}

				sendPoint.addEventListener(MessageFaultEvent.FAULT,messageFault);

				recivePoint.addEventListener(MessageFaultEvent.FAULT,messageFault);

			}

			

			public function sendPointMessage(p:People,e:MyEvent):void{

				var message:AsyncMessage=new AsyncMessage();

				message.body=new Object();

				message.body.name=p.name;

				message.body.x=e.x;

				message.body.y=e.y;

				sendPoint.send(message);

			}

			public function getPointMessageHandle(event:MessageEvent):void{

				trace("message");

//				throw new Error;

//				Alert.show("时间触发并且接受");

				//开始移动判断是否有name相同的数据,有就开始移动

				if(event.message.body.name!=firstName){

					Supporting(event);

				}

				

			}

			public function messageFault(e:MessageFaultEvent):void{

				trace(e.message);

			}

			//通过接受到的消息,其他角色开始移动,

			public function Supporting(event:MessageEvent):void{

				try{

				var p:People=getPeopleAsName(event.message.body.name);

				

//				throw new Error;

//				p.addEventListener(MyEvent.STOP,stopMoveAndUpdate);

				//People类没改,这相当于传参数,不是事件

				var me:MyEvent=new MyEvent(MyEvent.MOVING);

				me.x=event.message.body.x;

				me.y=event.message.body.y;

				if(p!=null){

					p.other(me);

					

					var role:Role=new Role();

					role.x=me.x;

					role.y=me.y;

					role.name=p.name;

					

//					updateRole.updateRole(role,p.name);

					//报服务器端的数据更新

					

					p.addEventListener(MyEvent.STOP,stopMoveAndUpdate);

					

				}

				}catch(e:Error){

					trace(e.message);

				}

			}

			//跟新服务器端的Role的信息

			

			public function stopMoveAndUpdate(e:MyEvent){

				trace("stop");

				

			}

			public function getPeopleAsName(name:String):People{

				var p:People=getChildByName(name) as People;

				return p;

			}

			

		]]>

	</mx:Script>

	

	<mx:Text x="240" y="20" text="当前在线" width="57"/>

	<mx:Text x="292" y="20" width="43" id="roleCount" text="{allRole.length}"/>

	<mx:TextInput x="40" y="626" width="34" id="dx" text="x坐标"/>

	<mx:TextInput x="96" y="626" width="39" id="dy" text="y坐标"/>

	<mx:Button x="143" y="626" label="initApp" click="initApp()" id="init"/>

	

	<mx:Script>

		<![CDATA[

			import mx.messaging.channels.StreamingAMFChannel;

			import mx.messaging.events.MessageAckEvent;

			import mx.messaging.events.ChannelFaultEvent;

			import mx.messaging.events.ChannelEvent;

			import mx.messaging.ChannelSet;

			import mx.messaging.channels.AMFChannel;

			import mx.messaging.channels.StreamingAMFChannel;

			import mx.messaging.events.MessageFaultEvent;

			

			import mx.messaging.messages.AsyncMessage;

			import mx.messaging.events.MessageEvent;

			public function initChannel(producer:Producer,consumer:Consumer){

				var myStreamingAMF:StreamingAMFChannel = new StreamingAMFChannel("my-streaming-amf", "messagebroker/streamingamf");

				var myPollingAMF:AMFChannel = new AMFChannel("my-polling-amf", "messagebroker/amfpolling");

				myPollingAMF.pollingEnabled = true;

				myPollingAMF.pollingInterval = 2000;

				var channelSet:ChannelSet = new ChannelSet();

				channelSet.addChannel(myStreamingAMF);

				channelSet.addChannel(myPollingAMF);

				consumer.channelSet = channelSet;				

				producer.channelSet = channelSet;

				producer.addEventListener(MessageFaultEvent.FAULT,sendError);

				consumer.addEventListener(MessageFaultEvent.FAULT,sendError);

				

			}

			/**

			 * 

			 * 

			 * **接受的信息是某个人进来的所发的信息;

			*/

			public function messagehandle(event:MessageEvent):void{

//				ta.text+=event.message.body+"\n";

				//发布一个通知,谁进来了

				come.text=event.message.body.msg+"";

				

				//把他添加到本数组allPeople中;

				addRoleToArray(event);

				

				

//				selectAllRole();//第一次登录的时候,后边人登录第一次登录的人看不到其他人,所以得查一次server

			}

			/**

			 * 添加角色到allRole数组

			 * 

			 * **/

			public function addRoleToArray(event:MessageEvent){

				var role:Role=new Role();

				role.x=event.message.body.x;

				role.y=event.message.body.y;

				role.name=event.message.body.name;

				

				//提交给服务器,

				//因为上边savePeople已经添加了,所以这就不用添加

//				getAll.addRole(role);

				//添加到本应用程序,

				//如果名字相同,是本程序的,所以不必要添加

				if(role.name!=firstName){

					addChildRole(role);

				}

			}

			

			

			/**向所有人广播,谁进来了。并且把这个角色的信息,发给所有客户端,客户端添加这个角色

			 * 并且还要发给服务器保存起来 

			 * **/

			public function sendMessage(role:Role):void{

				

				var message:AsyncMessage=new AsyncMessage();

//				message.body=name+"进入房间";

				message.body=new Object();

				message.body.msg=role.name+"进入房间";

				

				message.body.name=role.name;

				message.body.x=role.x;

				message.body.y=role.y;

				

				producer.send(message);

				

				trace("send");

				

			}

			public function sendError(e:MessageFaultEvent){

				trace(e.message);

			}

			

			public function resultArray(e:ResultEvent):void{

				

			}

			

			

		]]>

	</mx:Script>

	<mx:Producer id="producer" destination="chat">

		

	</mx:Producer>

	

	<mx:Consumer id="consumer" destination="chat" message="messagehandle(event)">

		

	</mx:Consumer>

	

	<mx:Text x="146" y="66" width="403" id="come" height="45" fontFamily="Arial" color="#F42E19" fontSize="19" text=""/><!--发送信息给每个client谁进来了-->

	<mx:TextInput x="218" y="626" width="79" id="fname" text="非中文名"/>

	

	<mx:Script>

		<![CDATA[

			

			public function addChildRole(role:Role):void{

				var peo:People=UtilPeopleRole.RoleToPeople1(role);

				if(peo.name!=firstName){

					addChild(peo);//添加到本数组

					allRole.addItem(role);//role也添加到本数组

					allPeople.addItem(peo);//peo也添加到本书组

					peo.addEventListener(MyEvent.MOVING,otherMove);

				}

			}

		]]>

	</mx:Script>

	<local:Producer_Consumer height="341" width="483" x="835" y="307">

	</local:Producer_Consumer>

</mx:Application>

 

 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics