面向对象游戏开发-键盘篇 |
| 作者:佚名 来源:zjs35blog 作者: zjs35 更新:2007-1-13 20:43:04 错误报告 我要投稿 |
|
主要是考虑到添加键盘事件的方便 (编者注:用光标键可使上下左右移动) 效果如下:
代码如下:
import fc.events.EventDispatcher; import fc.utils.Delegate; class fc.key.FKey { private var _isDowning:String; private var _keyCode:Number; private var addEventListener:Function; private var removeEventListener:Function; private var dispatchEvent:Function; private var id:Number; public function FKey(keyCode) { EventDispatcher.initialize(this); _keyCode = keyCode; init(); } private function init() { var obj = new Object(); var ins = this; obj.onKeyDown = function() { ins.down(); }; obj.onKeyUp = function() { ins.up(); }; Key.addListener(obj); id = setInterval(Delegate.create(this, event), 30); } private function down() { if (Key.getCode() == _keyCode) { _isDowning = "down"; } } private function up() { if (Key.getCode() == _keyCode) { _isDowning = "up"; } } private function event() { if (_isDowning == "down") { dispatchEvent({type:"onKeyDown", target:_keyCode,value:true}); } else if (_isDowning == "up") { dispatchEvent({type:"onKeyUp", target:_keyCode,value:false}); _isDowning = ""; } } public function get isDowning() { return _isDowning; } } //简单用法 import fc.key.FKey; import fc.utils.Delegate; class Test { private var left:FKey; private var right:FKey; private var top:FKey; private var bottom:FKey; private var mc:MovieClip; private var v=2 function Test(mc) { this.mc = mc; init(); } function init() { //四个方向键的key code left = new FKey(37); right = new FKey(39); top = new FKey(38); bottom = new FKey(40); left.addEventListener("onKeyDown", Delegate.create(this, leftMove)); right.addEventListener("onKeyDown", Delegate.create(this, rightMove)); top.addEventListener("onKeyDown", Delegate.create(this, topMove)); bottom.addEventListener("onKeyDown", Delegate.create(this, bottomMove)); } function leftMove(o) { mc._x-=v } function rightMove() { mc._x+=v } function topMove() { mc._y-=v } function bottomMove() { mc._y+=v } } //复杂一点的,如果用粒子系统制作加减速会更好 import fc.key.FKey; import fc.utils.Delegate; class Test { private var left:FKey; private var right:FKey; private var top:FKey; private var bottom:FKey; private var mc:MovieClip; var _vx = 0; var _vy = 0; var _a = 0.2; var _f = 0.96; var _m = 5; var _dx = 0; var _dy = 0; var _k:String; var _id; function Test(mc) { this.mc = mc; init(); } function init() { left = new FKey(37); right = new FKey(39); top = new FKey(38); bottom = new FKey(40); left.addEventListener("onKeyDown", Delegate.create(this, acce)); left.addEventListener("onKeyUp", Delegate.create(this, dece)); right.addEventListener("onKeyDown", Delegate.create(this, acce)); right.addEventListener("onKeyUp", Delegate.create(this, dece)); top.addEventListener("onKeyDown", Delegate.create(this, acce)); top.addEventListener("onKeyUp", Delegate.create(this, dece)); bottom.addEventListener("onKeyDown", Delegate.create(this, acce)); bottom.addEventListener("onKeyUp", Delegate.create(this, dece)); } function acce(o) { if ((o.target == 37 || o.target == 39) && _vx<_m) { _vx += _a; } if ((o.target == 38 || o.target == 40) && _vy<_m) { _vy += _a; } if (o.target == 37) { _dx = -1; } if (o.target == 38) { _dy = -1; } if (o.target == 39) { _dx = 1; } if (o.target == 40) { _dy = 1; } //还需几个同时按键的判断 mc._x += _vx*_dx; mc._y += _vy*_dy; } function dece() { var ins = this; mc.onEnterFrame = function() { trace([ins._vx, ins._vy]); ins._vx *= ins._f; ins._vy *= ins._f; this._x += ins._vx*ins._dx; this._y += ins._vy*ins._dy; if (Math.round(ins._vx) == 0 && Math.round(ins._vy) == 0) { delete this.onEnterFrame; } }; } }
|
|
| 文章录入:skyuu 责任编辑:skyuu |
|
| 【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |