·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3的EventDispatcher事件派发和碰撞源代码的例子

时间:2014-06-13 16:03酷播
[AS3]as3的EventDispatcher事件派发和碰撞源代码的例子

[AS3]as3的EventDispatcher事件派发和碰撞源代码的例子

  1. package 
  2. { 
  3.     import flash.display.Sprite; 
  4.     import flash.events.Event; 
  5.     import flash.geom.Point; 
  6.     /** 
  7.      * @author  SinSoul 
  8.      * @http://www.cnblogs.com/sinsoul 
  9.     **/ 
  10.     public class Main extends Sprite  
  11.     { 
  12.         private var ballsArray:Array; 
  13.         public function Main():void { 
  14.             ballsArray = []; 
  15.             var _i:uint = 30; 
  16.             while (_i--) { 
  17.                 var _ball:Dispatch = new Dispatch; 
  18.                 _ball.x = Math.random () * stage.stageWidth; 
  19.                 _ball.y = Math.random () * stage.stageHeight; 
  20.                                                     //这里就是监听的地方 
  21.                 addChild (_ball).addEventListener (Dispatch.Dispatch_MOVE,DispatchMove ); 
  22.                 ballsArray .push (_ball); 
  23.             } 
  24.         } 
  25.         private function DispatchMove(e:Event ):void { 
  26.             var _ball:Dispatch = e.target as Dispatch; 
  27.             var _array:Array = ballsArray .concat (); 
  28.             _array.splice (_array.indexOf (_ball), 1); 
  29.             for each (var _b:Dispatch in _array) { 
  30.                 if (Point.distance(new Point(_b.x, _b.y), new Point(_ball.x, _ball.y)) < 20) { 
  31.                     _b_ball.color = _b.color = 0xff0000; 
  32.                     ballsArray.splice (ballsArray.indexOf (_ball), 1); 
  33.                     ballsArray.splice (ballsArray.indexOf (_b), 1); 
  34.                     //return; 
  35.                 } 
  36.             } 
  37.         } 
  38.     } 
  39.       
  40. } 

[AS3]as3的EventDispatcher事件派发和碰撞源代码的例子

  1. package 
  2. { 
  3.     import flash.display.Sprite; 
  4.     import flash.events.Event; 
  5.     import flash.events.MouseEvent; 
  6.     import flash.geom.ColorTransform; 
  7.       
  8.     /** 
  9.      * @author  SinSoul 
  10.      * @http://www.cnblogs.com/sinsoul 
  11.     **/ 
  12.     public class Dispatch extends Sprite  
  13.     { 
  14.         public static const Dispatch_MOVE:String = "DispatchMove"; 
  15.         public function Dispatch():void { 
  16.             stage?init(null):addEventListener (Event.ADDED_TO_STAGE, init); 
  17.         }       
  18.         private function init(e:Event):void 
  19.         { 
  20.             graphics.beginFill(0x00); 
  21.             graphics.drawCircle(0, 0, 10); 
  22.             buttonMode = true; 
  23.             addEventListener (MouseEvent.MOUSE_DOWN, mouseDown); 
  24.             // 
  25.             removeEventListener(Event.ADDED_TO_STAGE, init); 
  26.               
  27.         } 
  28.         //自己不能做的事,发出事件,请求有管辖权的对象去做 
  29.           //这里是发出事件的地方 
  30.         private function mouseMove(e:MouseEvent ):void { 
  31.             dispatchEvent (new Event(Dispatch.BALL_MOVE)); 
  32.         } 
  33.         //自己能做的事,自己做 
  34.         private function mouseDown(e:MouseEvent ):void { 
  35.             startDrag (); 
  36.             stage.addEventListener (MouseEvent.MOUSE_MOVE, mouseMove ); 
  37.             stage.addEventListener (MouseEvent.MOUSE_UP, mouseUp); 
  38.         } 
  39.         private function mouseUp(e:MouseEvent ):void { 
  40.             stopDrag (); 
  41.             stage.removeEventListener (MouseEvent.MOUSE_MOVE, mouseMove ); 
  42.             stage.removeEventListener (MouseEvent.MOUSE_UP, mouseUp); 
  43.         }       
  44.         public function set color(_c:uint):void { 
  45.             var _ctf:ColorTransform = new ColorTransform; 
  46.             _c_ctf.color = _c; 
  47.             transform.colorTransform = _ctf; 
  48.             // 
  49.             stopHandler(); 
  50.         } 
  51.         private function stopHandler():void { 
  52.             mouseUp(null); 
  53.             buttonMode = false; 
  54.             removeEventListener (MouseEvent.MOUSE_DOWN, mouseDown); 
  55.         } 
  56.     } 
  57.       
  58. } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器