| 首页 | 新闻 | 网页 | 设计 | 色彩 | 原创 | 视觉 | 素材 | 动漫 | 酷站 | 策划 | 文案 | 访谈 | 运营 | 编程 | 数据库 | 服务器 | 下载 | 图库 | 
您的位置: 幽幽天空 > 网页 > 网页制作 > Flash教程 > Flash游戏制作 > 文章正文 用户登录
Flash面向对象程序
关于color对象的使
MC和对象数据类型
电影剪辑对象的绘
关于面向对象编程
应用Key对象控制元
TextSnapshot对象
数学对象的应用
AS2.0面向对象编程
AS基础精典教程 第

面向对象游戏开发-键盘篇           

面向对象游戏开发-键盘篇

作者:佚名 来源: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 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    发表评论:
    姓名:  评 分: 1分 2分 3分 4分 5分
     
  • 严禁发表危害国家安全、政治、黄色淫秽等内容的评论。
  • 用户需对自己在使用幽幽天空服务过程中的行为承担法律责任。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表机友个人观点,与本网站立场无关。