WEB开发网
开发学院图形图像Flash Flash AS3教程:Direction类和Dot类 阅读

Flash AS3教程:Direction类和Dot类

 2008-06-05 11:22:07 来源:WEB开发网   
核心提示: clear方法:public function clear():void清除所有方向记录area属性:public var area:InteractiveObject返回作用区域sole属性:public var sole:Boolean返回是否单向操作DirectionEvent.DO

clear方法:

public function clear():void

清除所有方向记录

area属性:

public var area:InteractiveObject

返回作用区域

sole属性:

public var sole:Boolean

返回是否单向操作

DirectionEvent.DO事件:

当有方向键是按下去的时候,则会发布事件,事件中含有up,down,left,right,4个属性,分别表示哪几个键是按下去的!

========== 气 死 你 的 分 割 线 ==========

Dot类在前面的整理笔记中,曾经说过,这次是更新类的方法和属性

增加了旋转属性,并且可以计算当前方向的某距离后的点

以下只对更新的方法和属性进行讲解:其他的请看老的整理笔记:

go方法:

public function go(num:Number,isChange:Boolean = false):Dot

参数一,表示面向旋转方向前进多少的距离

参数二,表示是否也跟新该点基于num变化之后的点坐标

clear方法:

public function clear():void

清空绑定对象的引用

r 属性:

public function set r(num:Number):void

public function get r():Number

旋转属性的设置,如果isListener值为真,则改变旋转值会触发R_CHANGE的事件

Direction类源代码:

CODE:

package index.base.game{
    
    import flash.events.EventDispatcher;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.display.InteractiveObject;
    
    import index.base.events.DirectionEvent;
    
    public class Direction extends EventDispatcher{
        
        //方向表示
        public static const UP:uint = 0;
        public static const DOWN:uint = 1;
        public static const LEFT:uint = 2;
        public static const RIGHT:uint = 3;
        
        //作用区域
        public var area:InteractiveObject;
        //是否单向
        public var sole:Boolean;
        
        //上下左右键值
        private const directionAr:Array = new Array(4);
        
        //是否上下左右
        private var _up:Boolean = false;
        private var _down:Boolean = false;
        private var _left:Boolean = false;
        private var _right:Boolean = false;
        
        public function Direction(_area:InteractiveObject,isSole:Boolean = false,_up:uint = 38,_down:uint = 40,_left:uint = 37,_right:uint = 39){
            area = _area;
            sole = isSole;
            directionAr[UP] = _up;
            directionAr[DOWN] = _down;
            directionAr[LEFT] = _left;
            directionAr[RIGHT] = _right;
            start();
        }
        
        //开始获取事件
        public function start():void{
            area.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
            area.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
            area.addEventListener(Event.ENTER_FRAME,onEnterFrame);
        }
        
        //事件帧频繁触发
        private function onEnterFrame(e:Event):void{
            var num:uint = Number(_up) + Number(_down) + Number(_left) + Number(_right);
            if(num == 0){
                return;
            }
            
            var eve:DirectionEvent = new DirectionEvent(DirectionEvent.DO);
            eve.up = _up;
            eve.down = _down;
            eve.left = _left;
            eve.right = _right;
            dispatchEvent(eve);
        }
        
        //停止获取事件
        public function stop():void{
            area.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
            area.removeEventListener(KeyboardEvent.KEY_UP,onKeyUp);
            area.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
        }
        
        //鼠标按下去事件
        private function onKeyDown(e:KeyboardEvent):void{
            key(e.keyCode,true)
        }
        
        //鼠标弹上来事件
        private function onKeyUp(e:KeyboardEvent):void{
            key(e.keyCode,false)
        }
        
        //变化状态
        private function key(num:uint,isDown:Boolean):void{
            switch(num){
                case directionAr[UP]:
                    if(sole) clear();
                    _up = isDown;
                break;
                case directionAr[DOWN]:
                    if(sole) clear();
                    _down = isDown;
                break;
                case directionAr[LEFT]:
                    if(sole) clear();
                    _left = isDown;
                break;
                case directionAr[RIGHT]:
                    if(sole) clear();
                    _right = isDown;
                break;
            }
        }
        
        //设置按钮
        public function setKey(num:uint,vars:uint):void{
            directionAr[num] = vars;
        }
        
        //清空按键
        public function clear():void{
            _up = _down = _left = _right = false;
        }
    }
}

Tags:Flash AS 教程

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接