Flash游戏、Flash音乐、小游戏

加入收藏

设为主页

首页 | Flash游戏 | Flash音乐 | Flash动画 | 邻家女孩 | 动漫下载 | 小说 | 动漫资讯 | 免费博客

Flash游戏 | 动漫COSPLAY | 手机世界 | 游戏新闻 | IT业界 | 动漫设计 | 软硬件交流

首页>>动漫设计>>正文面向对象游戏开发-键盘篇

将woogood加入收藏夹 | 将woogood设置成为你电脑首页

搜索 SEARCH

    

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

作者:[] 来源:[] [2005-10-6 16:46:23] [对"面向对象游戏开发-键盘篇"发表评论]

最新文章

·隐藏卸载信息:跟入侵者玩躲猫猫
·网络新威胁 无线局域网入侵检测现
·宽带用户易被攻击 用户需注意七大
·如何查找和清除线程插入式木马程
·日志分析两部曲:充分利用日志保
·安全设置Windows组策略 有效阻止
·实现了!中文WinXP安装IE7 Beta1
·黑客破解Email账号常用的三种方法
·基础知识 XST攻击理论及手法讲解
·威胁同样巨大 基于Telnet协议的攻

推荐文章

·请教看电影多的人问题
·文字处理软件疑难问题求助,
·求助:Excel问题
·在线等...........
·大家都是怎样用摄像头的?发
·如何加启动程序?急
·midia player classic使用时
·关于IE,请大家帮助
·IE被改用兔子也没用啊??
·播放器问题,大家帮帮忙啊

主要是考虑到添加键盘事件的方便
(编者注:用光标键可使上下左右移动)
效果如下:


代码如下:
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;
            }
        };
    }
}

相关文章:

对"面向对象游戏开发-键盘篇"发表评论:

热门Flash 最新Flash 热门小说 最新小说

 

Copyright@2005-2006 精品Flash www.WooGood.com All Right Reserved

联系信箱: gold1686tom.com