[AS3]as3的EventDispatcher事件派发和碰撞源代码的例子
[AS3]as3的EventDispatcher事件派发和碰撞源代码的例子
[AS3]as3的EventDispatcher事件派发和碰撞源代码的例子
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.geom.Point;
- /**
- * @author SinSoul
- * @http://www.cnblogs.com/sinsoul
- **/
- public class Main extends Sprite
- {
- private var ballsArray:Array;
- public function Main():void {
- ballsArray = [];
- var _i:uint = 30;
- while (_i--) {
- var _ball:Dispatch = new Dispatch;
- _ball.x = Math.random () * stage.stageWidth;
- _ball.y = Math.random () * stage.stageHeight;
- //这里就是监听的地方
- addChild (_ball).addEventListener (Dispatch.Dispatch_MOVE,DispatchMove );
- ballsArray .push (_ball);
- }
- }
- private function DispatchMove(e:Event ):void {
- var _ball:Dispatch = e.target as Dispatch;
- var _array:Array = ballsArray .concat ();
- _array.splice (_array.indexOf (_ball), 1);
- for each (var _b:Dispatch in _array) {
- if (Point.distance(new Point(_b.x, _b.y), new Point(_ball.x, _ball.y)) < 20) {
- _b_ball.color = _b.color = 0xff0000;
- ballsArray.splice (ballsArray.indexOf (_ball), 1);
- ballsArray.splice (ballsArray.indexOf (_b), 1);
- //return;
- }
- }
- }
- }
- }
[AS3]as3的EventDispatcher事件派发和碰撞源代码的例子
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.geom.ColorTransform;
- /**
- * @author SinSoul
- * @http://www.cnblogs.com/sinsoul
- **/
- public class Dispatch extends Sprite
- {
- public static const Dispatch_MOVE:String = "DispatchMove";
- public function Dispatch():void {
- stage?init(null):addEventListener (Event.ADDED_TO_STAGE, init);
- }
- private function init(e:Event):void
- {
- graphics.beginFill(0x00);
- graphics.drawCircle(0, 0, 10);
- buttonMode = true;
- addEventListener (MouseEvent.MOUSE_DOWN, mouseDown);
- //
- removeEventListener(Event.ADDED_TO_STAGE, init);
- }
- //自己不能做的事,发出事件,请求有管辖权的对象去做
- //这里是发出事件的地方
- private function mouseMove(e:MouseEvent ):void {
- dispatchEvent (new Event(Dispatch.BALL_MOVE));
- }
- //自己能做的事,自己做
- private function mouseDown(e:MouseEvent ):void {
- startDrag ();
- stage.addEventListener (MouseEvent.MOUSE_MOVE, mouseMove );
- stage.addEventListener (MouseEvent.MOUSE_UP, mouseUp);
- }
- private function mouseUp(e:MouseEvent ):void {
- stopDrag ();
- stage.removeEventListener (MouseEvent.MOUSE_MOVE, mouseMove );
- stage.removeEventListener (MouseEvent.MOUSE_UP, mouseUp);
- }
- public function set color(_c:uint):void {
- var _ctf:ColorTransform = new ColorTransform;
- _c_ctf.color = _c;
- transform.colorTransform = _ctf;
- //
- stopHandler();
- }
- private function stopHandler():void {
- mouseUp(null);
- buttonMode = false;
- removeEventListener (MouseEvent.MOUSE_DOWN, mouseDown);
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...