flex做的人物移动和多人同步

人物移动,是把一张图代码割开,然后通过鼠标和人物的角度判断,显示哪张图片

多人同步,我只是用red5服务器(因为不熟悉),我只是用他保存了sharedObject,服务器只是给定目标坐标

client多个人物移动,不需要移动一个坐标就告诉服务器。服务器只是给定你目标坐标,到底怎么移动,还是让客户端来完成

package simple
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;

	public class People extends UIComponent
	{
		/**
//		 * 图片宽540,高864 一行9张图,共8行
		 * **/
		[Embed(source="../image/h1.png")]
		private var people:Class;
		
		private var WIDTH:int=80;
		private var HEIGHT:int=91;
		private var ROW:int=8;//8hang 
		private var COL:int=8;//每行8个角色
		
		private var peo:Bitmap;//就是people
		
		private var image:Point;//按照任务计算出来的实际脚底的坐标
		
		private var maps:Array=new Array();//或得各个人物不同的坐标
		private var mousex:int;
		private var mousey:int;
		private var speed:int=3;
		private var count:int;
		public var arr:Array=[];//取 移动某个方向的各个人物动作;
		
		private var test:int=1;//图片变换太快,用这个变量改下

		
		
		
		public function get Peo():Bitmap{
			return peo;
		}
		public function People()
		{
			
			peo=new people() as Bitmap;

			init();
			
			this.addChild(peo);
//			this.addEventListener(MyEvent.MOVING,other);
		
		}
		public function other(e:MyEvent):void{
			trace("myevent");
			this.mousex=e.x;
			this.mousey=e.y;
			image.x=peo.x+WIDTH/2;
			image.y=peo.y+HEIGHT;
			var obj:Object=this.moveAngle();
			getMoveImage(obj.angle);
//			movePeople();
			this.addEventListener(Event.ENTER_FRAME,movePeople);
		}
		
		
		public function init():void{
		
			getAllPeople();//割图
			peo.bitmapData=maps[0][0];//初始化时  在第4行第1个人物
			this.addChild(peo);
			peo.x=300;
			peo.y=300;
			
			image=new Point();
			image.x=(peo.x+WIDTH/2);
			image.y=peo.y+HEIGHT;
			
			//全局监听
//			Application.application.addEventListener(MouseEvent.CLICK,moves);
			
		}
		public function getAllPeople():Array{
			for(var i:int=0;i<ROW;i++)
			{
				var array:Array=new Array();
				for(var j:int=0;j<COL;j++){
				var bit:BitmapData = new BitmapData(WIDTH,HEIGHT,true,0);
				
				var mat:Matrix = new Matrix();
				mat.tx = -j*WIDTH;//平移多少 
				mat.ty = -i*HEIGHT;//竖移动多少

				bit.draw(peo,mat,null,null,new Rectangle(0,0,WIDTH,HEIGHT));
				array.push(bit);
				}
				maps.push(array);
			
			}
			return maps;
		}
		public function moves(event:MouseEvent):void{
			trace("moves++++++");
			var me:MyEvent=new MyEvent(MyEvent.MOVING);
			me.x=mouseX;
			me.y=mouseY;
			this.dispatchEvent(me);
			this.mousex=mouseX;
			this.mousey=mouseY;
//			image.x=peo.x+WIDTH/2;
//			image.y=peo.y+HEIGHT;
			var obj:Object=this.moveAngle();
			getMoveImage(obj.angle);

			
//			movePeople();
			this.addEventListener(Event.ENTER_FRAME,movePeople);
			
		}
		public function movePeople(event:Event):void{
			image.x=peo.x+WIDTH/2;
			image.y=peo.y+HEIGHT;
			var ylength:Number=mousey-image.y;
			var xlength:Number=mousex-image.x;
			var dist:int=Math.sqrt(xlength*xlength+ylength*ylength);
			var angle:Number=Math.atan2(ylength,xlength);
			var vx:Number=Math.cos(angle)*speed;
	     	var vy:Number=Math.sin(angle)*speed;
	     	peo.x+=vx;
	     	peo.y+=vy;
	    
	     	test=test+1;
	     	if(test==2){
	     		count=count+1;
	     		if(count==8){
	     			count=0;
	     		}
	     		test=0;
	     	}
	     	
	     	
	     	peo.bitmapData=arr[count];
	     	
	     	trace("length"+dist);
			if(dist<15){
				peo.bitmapData=arr[0]
				this.removeEventListener(Event.ENTER_FRAME,movePeople);
				this.count=0;
				trace("remove");
				return;
			}
		}
		
		public function getMoveImage(angle:Number):void{ //得到 要移动的方向的数组
			
			var angle:Number=Math.abs((angle)*180/Math.PI);
			
			if(mousex>=image.x&&mousey<=image.y){//第一象限
				getBearing(angle,2,7,3);
				
			}else if(mousey<=image.y&&mousex<=image.x){//第2象限
				getBearing(angle,1,6,3);  //mousex=201mousey=342
				
			}else if(mousey>=image.y&&mousex<=image.x){//第3象限
				getBearing(angle,1,4,0);
				
			}else if(mousey>=image.y&&mousex>=image.x){ //第4象限
				getBearing(angle,2,5,0);
				
			}
			
		}
		/**把一个象限分成3分22.5,22.5~67.5,67.5~90我取整了**/
		public function getBearing(angle:Number,first:int,second:int,three:int):void{
			if(angle<23){
				arr=maps[first];
			}else if(angle>=23&&angle<68){
				arr=maps[second];
			}else if(angle>=68){
				arr=maps[three];
			}
		}
		
	
//		/**
//		 * 计算鼠标点击后角色与人物之间的角度,(来输出相应的角色,完成走动)
//		 * @Return int 
//		 * */
		public function moveAngle():Object{ 
			 var obj:Object={};
			 var ylength:Number=mousey-image.y;//求出距离  y的距离
			 
			 var xlength:Number=mousex-image.x;//求出距离  x的距离
			 
			 var dist:Number=Math.sqrt(xlength*xlength+ylength*ylength);//求出距离
			
//			 var sinValue:Number=(ylength/dist);//求出对边比斜边的比值 
//			 
//			 var angle:Number=Math.asin(sinValue);//根据比值 求出角度
		     var angle:Number=Math.asin(ylength/dist);
//			 trace("ttttt:::"+angle*180/Math.PI);
			 var obj:Object={angle:angle,dist:dist,
			   atan:Math.atan2(ylength,xlength)};
			 return obj;
		}
//		
		
	}
}
 
package simple
{
	import flash.display.BitmapData;
	import flash.events.Event;

	public class MyEvent extends Event
	
	{
		public static const MOVING:String="moving";
		public var x:Number;
		public var y:Number;
		public var bit:BitmapData;
		public function MyEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
		{
			super(type, bubbles, cancelable);
		}
		
	}
}

 <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:com="com.*" xmlns:simple="simple.*" creationComplete="init()">
    
    <simple:People width="100%" height="100%" id="yong" x="100" y="100">
        
    </simple:People>
    <simple:People width="100%" height="100%" x="50" y="150" id="kang">
        
    </simple:People>
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import flash.net.SharedObject;
//            import mx.logging.Log;
            import simple.People;
            import simple.MyEvent;
            import mx.controls.Alert;
            import flash.display.BitmapData;
            
            private var net:NetConnection;
            private var soChat:SharedObject;
            private var clientId:int=0;
            private var str:String;
            private var arr:ArrayCollection
            
            
            private var urlvar:String
            
            //保存所有people信息;
            private static var allPeople:ArrayCollection
            
            private var people:People;
            
            public function init():void{
                kang.Peo.bitmapData=kang.getAllPeople()[1][0];
                var url:String=Application.application.url;
                var sp:Array=url.split("?");
                var variable:String=sp[1];
                urlvar=variable;
                trace(variable+"sdssd");
                if(urlvar=="id=1"){
                    Application.application.addEventListener(MouseEvent.CLICK,yong.moves);
                }else{
                    Application.application.addEventListener(MouseEvent.CLICK,kang.moves);
                }
                NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
                SharedObject.defaultObjectEncoding  = flash.net.ObjectEncoding.AMF0;    
                net=new NetConnection();
                net.connect("rtmp://127.0.0.1/red");//我用的是red5,/ red是我的一个随便的应用

                //你改成其他都行不是red也行,反正是个应用就行


                addEvent();
                yong.addEventListener(MyEvent.MOVING,peopelMove1);
                kang.addEventListener(MyEvent.MOVING,peopelMove2);
            }
            public function addEvent():void{
                net.addEventListener(NetStatusEvent.NET_STATUS,showError);
                soChat = SharedObject.getRemote( "movePeople1", net.uri, true );
                soChat.client = this;
                soChat.connect(net);
            }

            public function showError(e:NetStatusEvent):void{
                trace(e.info.code);
                trace(e.info.code=="NetConnection.Connect.Success");
            }
            private function peopelMove1(e:MyEvent):void{
                soChat.send("setPoint1",e.x,e.y);
                trace("set1");
            }
            
            private function peopelMove2(e:MyEvent):void{
                
                soChat.send("setPoint2",e.x,e.y);
                trace("set2");
            }
            public function setPoint1(x:Number,y:Number):void{
                var me:MyEvent=new MyEvent(MyEvent.MOVING);
                me.x=x;
                me.y=y;
                yong.other(me);
            }
            public function setPoint2(x:Number,y:Number):void{
                trace("setPoint2");
                var me:MyEvent=new MyEvent(MyEvent.MOVING);
                me.x=x;
                me.y=y;
                kang.other(me);
            }
    
        ]]>
    </mx:Script>
    
</mx:Application>

你可能感兴趣的:(应用服务器,.net,Flex,Flash,Adobe)