2016-10-13 00:16:48 点击量:2 标签: 收藏本文
[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);
- }
- }
- }
我们是一家提供综合软件外包与弱电工程服务的技术公司。
业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。
公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室