[Socket+AMF3]给合Flash的Socket和AMF3来尝试开发web游戏引擎

  今天尝试用Socket和AMF3来开发web游戏引擎,用Flash的好处就是因为插件普及率高,无需用户另行安装,所以用Flash来开发丰富的Web游戏成为很好的选择。服务端仍然采用apache mina+AMF3来写Socket服务程序。
演示效果图:


客户端代码:游戏角色类

  1. /**  
  2. * ...  
  3. * @author Kinglong  
  4. * @version 0.1  
  5. */   
  6.   
  7. package  project.test {  
  8.   
  9.     import  flash.display.*;  
  10.     import  flash.events.*;  
  11.       
  12.     import  gs.TweenLite;  
  13.       
  14.     public   class  GameRole  extends  Sprite {            
  15.         private  var _id:uint;  
  16.         private  var tween:TweenLite;  
  17.         public  function GameRole(id:uint,self:Boolean= false ) {  
  18.             mouseChildren = false ;  
  19.             mouseEnabled = false ;  
  20.             _id = id;  
  21.             if (self){  
  22.                 this .graphics.beginFill( 0xFF0000 );                
  23.             }else  {  
  24.                 this .graphics.beginFill( 0x000000 0.6 );               
  25.             }  
  26.             this .graphics.drawRoundRect( - 10 , - 10 20 20 10 10 );  
  27.             this .graphics.endFill();  
  28.         }  
  29.           
  30.         public  function move(x:Number,y:Number): void  {  
  31.             remove();  
  32.             this .x = x;  
  33.             this .y = y;  
  34.         }  
  35.           
  36.         public  function to(x:Number, y:Number): void  {  
  37.             remove();  
  38.             tween = TweenLite.to(this 1 , { x:x, y:y } );  
  39.         }  
  40.           
  41.         public  function get id():uint {  
  42.             return  _id;  
  43.         }  
  44.           
  45.         private  function remove(): void  {  
  46.             if  (tween !=  null ) {  
  47.                 TweenLite.removeTween(tween);  
  48.                 tween = null ;  
  49.             }  
  50.         }  
  51.           
  52.           
  53.     }  
  54.       
  55. }  



文档类

  1. /**  
  2. * ...  
  3. * @author Kinglong  
  4. * @version 0.1  
  5. */   
  6.   
  7. package  project.test {  
  8.   
  9.     import  flash.display.*;  
  10.     import  flash.events.*;  
  11.     import  flash.net.Socket;  
  12.     import  flash.net.ObjectEncoding;  
  13.     import  flash.text.TextField;  
  14.     import  flash.utils.ByteArray;  
  15.     import  flash.utils.Dictionary;  
  16.       
  17.     import  com.klstudio.data.Map;  
  18.       
  19.     public   class  TestGame  extends  Sprite {    
  20.         private  var _socket:Socket;  
  21.         private  var _id:uint;  
  22.         private  var _lists:Map;  
  23.         private  var _info:TextField;  
  24.         public  function TestGame() {              
  25.             _info = new  TextField();  
  26.             _info.selectable = false ;             
  27.             _info.text = "未连接" ;  
  28.             addChild(_info);  
  29.               
  30.             stage.addEventListener(MouseEvent.MOUSE_UP, handler);             
  31.             _lists = new  Map();  
  32.             _socket = new  Socket();  
  33.             _socket.objectEncoding = ObjectEncoding.AMF3;  
  34.             configureListeners(_socket);  
  35.             _socket.connect("localhost" 110 );            
  36.         }     
  37.           
  38.         private  function configureListeners(dispatcher:IEventDispatcher): void  {  
  39.             dispatcher.addEventListener(Event.CLOSE, handler);  
  40.             dispatcher.addEventListener(Event.CONNECT, handler);  
  41.             dispatcher.addEventListener(IOErrorEvent.IO_ERROR, handler);  
  42.             dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handler);  
  43.             dispatcher.addEventListener(ProgressEvent.SOCKET_DATA, handler);  
  44.         }  
  45.           
  46.         private  function handler(event:Event): void  {  
  47.             switch (event.type) {  
  48.                 case  ProgressEvent.SOCKET_DATA:  
  49.                     var bytes:ByteArray = new  ByteArray();  
  50.                     _socket.readBytes(bytes);  
  51.                     bytes.uncompress();  
  52.                     receive(bytes.readObject());  
  53.                     break ;  
  54.                 case  Event.CLOSE:  
  55.                     debug("连接关闭" );  
  56.                     removeAll();  
  57.                     break ;  
  58.                 case  Event.CONNECT:  
  59.                     debug("连接中..." );  
  60.                     break ;  
  61.                 case  IOErrorEvent.IO_ERROR:  
  62.                 case  SecurityErrorEvent.SECURITY_ERROR:  
  63.                     debug("连接失败" );  
  64.                     break ;  
  65.                 case  MouseEvent.MOUSE_UP:  
  66.                     var mEvent:MouseEvent = event as MouseEvent;                      
  67.                     send(this .mouseX,  this .mouseY);  
  68.                     break ;  
  69.             }  
  70.         }  
  71.           
  72.         private  function send(x:Number, y:Number): void  {  
  73.             if  (!_socket.connected) {  
  74.                 return ;  
  75.             }  
  76.             var bytes:ByteArray = new  ByteArray();  
  77.             bytes.writeObject( { event:"move" , x:x, y:y } );  
  78.             bytes.compress();  
  79.             _socket.writeBytes(bytes);  
  80.             _socket.flush();  
  81.         }  
  82.           
  83.         private  function receive(object:Object): void  {  
  84.             switch (object.event) {  
  85.                 case   "init" :  
  86.                     init(object.id);  
  87.                     for  (var i:uint =  0 ; i < object.list.length; i++ ) {  
  88.                         var item:Object = object.list[i];  
  89.                         add(item.id, item.x, item.y);  
  90.                     }  
  91.                     break ;  
  92.                 case   "move" :  
  93.                     move(object.id, object.x, object.y);                      
  94.                     break ;  
  95.                 case   "remove" :  
  96.                     remove(object.id);                    
  97.                     break ;  
  98.                 case   "add" :  
  99.                     add(object.id);  
  100.                     break ;  
  101.             }  
  102.         }  
  103.           
  104.         private  function init(id:uint): void  {  
  105.             this ._id = id;  
  106.             add(id);  
  107.         }  
  108.           
  109.         private  function add(id:uint, x:Number = undefined, y:Number = undefined): void  {              
  110.             x = x || stage.stageWidth / 2 ;  
  111.             y = y || stage.stageHeight / 2 ;  
  112.             if  (_lists.containsKey(id)) {  
  113.                 return ;  
  114.             }  
  115.             var role:GameRole = new  GameRole(id,_id == id);  
  116.             role.move(x,y);  
  117.             addChild(role);  
  118.             _lists.put(id, role);  
  119.         }  
  120.           
  121.         private  function move(id:uint, x:Number, y:Number): void  {  
  122.             if  (_lists.containsKey(id)) {  
  123.                 var role:GameRole = _lists.get(id);  
  124.                 role.to(x, y);  
  125.             }  
  126.         }  
  127.           
  128.         private  function remove(id:uint): void  {  
  129.             if  (_lists.containsKey(id)) {  
  130.                 var role:GameRole = _lists.remove(id);  
  131.                 trace(role);  
  132.                 if  ( this .contains(role)) {  
  133.                     removeChild(role);                    
  134.                 }  
  135.                 role = null ;  
  136.             }  
  137.         }  
  138.           
  139.         private  function removeAll(): void  {  
  140.             var keys:Array = _lists.keys();  
  141.             for  (var i:uint =  0 ; i < keys.length; i++ ) {  
  142.                 var role:GameRole = _lists.remove(keys[i]);               
  143.                 if  ( this .contains(role)) {  
  144.                     removeChild(role);  
  145.                 }  
  146.                 role = null ;                  
  147.             }  
  148.         }  
  149.           
  150.         private  function debug(msg:String): void  {  
  151.             _info.text = msg;  
  152.             trace(msg);  
  153.         }  
  154.           
  155.     }  
  156.       
  157. }  



这个尝试的游戏引擎功能很简单的,接下来可以加上更丰富的功能了 ...

你可能感兴趣的:(游戏,Web,socket,Flash,Mina)