欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Flash AS3教程:Direction类和Dot类

程序员文章站 2022-07-11 10:02:20
前面讲述了Flash as3教程:OutDisplay类,本篇对Direction类的方法属性和Dot的更新部分讲解。 关于该例子的教程请关注第九篇笔记! 即将出Direction类与Dot类的实战使用教程,本篇... 08-10-06...
前面讲述了flash as3教程:outdisplay类,本篇对direction类的方法属性和dot的更新部分讲解。
关于该例子的教程请关注第九篇笔记!
即将出direction类与dot类的实战使用教程,本篇只对direction类的方法属性和dot的更新部分讲解

首先是
index.base.game.direction类
作用:控制飞机游戏,坦克游戏,或者一些和方向有关的方向按键操作

构造函数:
public function direction(_area:interactiveobject,issole:boolean = false,_up:uint = 38,_down:uint = 40,_left:uint = 37,_right:uint = 39)
参数一:方向键的作用区域,如果_area当前不是焦点,那么是侦听不到键盘事件的,一般这儿都是使用stage做为作用区域
参数二:是否为单向事件触发,如果为false,那么按了什么键就是什么,可以同时触发上和左等两个或者两个以上的事件,反之以最后按的那个键为准
参数三,四,五,六:按键的键值,默认为38,40,37,39,分别是方向键的上下左右!

start方法:
public function start():void
开始捕获事件,当触发构造函数,将自动执行start方法

stop方法:
public function stop():void
停止捕获事件

setkey方法:
public function setkey(num:uint,vars:uint):void
设置按键键值
参数一:方向键标识,请参考该类的常量属性
参数二:按键键值

常量属性:
public static const up:uint = 0;
public static const down:uint = 1;
public static const left:uint = 2;
public static const right:uint = 3;
分别代表:上下左右的方向键标识

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;
}
}
}
dot类源代码:

code:
package index.base.geom{

import flash.events.eventdispatcher;
import flash.display.displayobject;

import index.base.events.dotevent;

public class dot extends eventdispatcher{

private var _x:number;
private var _y:number;
private var _r:number;
private var dis:displayobject;

public var islisten:boolean;

public function dot(x_:number = 0,y_:number = 0,r_:number = 0,_islisten:boolean = false){
_x = x_;
_y = y_;
_r = r_;
islisten = _islisten;
}

//绑定displayobject
public function bind(_dis:displayobject,isintime:boolean = false):void{
dis = _dis;
updata();
if(isintime) dis.addeventlistener("enterframe",enterframefun);
}

//帧频繁事件
private function enterframefun(e:object):void{
if(_x != dis.x) x = dis.x;
if(_y != dis.y) y = dis.y;
if(_r != dis.rotation) r = dis.rotation;
}

//更新xy数据
public function updata():void{
if(dis != null){
_x = dis.x;
_y = dis.y;
_r = dis.rotation;
}
}

//计算该点向r方向前进某距离后的点
public function go(num:number,ischange:boolean = false):dot{
updata();
var yx:number = math.tan(_r * math.pi / 180);
var tmpx:number = num / math.sqrt(math.pow(yx,2) 1);
var tmpy:number = tmpx * yx;
var n:int = number(math.abs(_r) <= 90) * 2 - 1;
var dot:dot = new dot(_x tmpx * n,_y tmpy * n,_r);
if(ischange){
x = dot.x;
y = dot.y;
}
return dot;
}

//计算该点与另外一点的距离
public function from(_dot:dot,isquadrant:boolean = false):number{
updata();
var num:number = math.sqrt(math.pow(_dot.x - _x,2) math.pow(_dot.y - _y,2));
if(!isquadrant) num = math.abs(num);
return num;
}

//计算该点与另外一点所形成的线段与水平线的夹角,按顺时间计算
public function angle(_dot:dot,isradian:boolean = false):number{
updata();
var numx:number = _dot.x - _x;
var numy:number = _dot.y - _y;
var num:number = math.atan(numy/numx);
if(!isradian) num = num * 180 / math.pi;
return num;
}

//返回当前点处在另外一点的哪个象限中 或 返回另外一点处在当前点的哪个象限中
public function quadrant(_dot:dot,ismaster:boolean = true):int{
updata();
if(_x == _dot.x || _y == _dot.y){
return 0;
}

var num:int;
var p1:boolean = (_x - _dot.x) > 0;
var p2:boolean = (_y - _dot.y) > 0;
num = ismaster ? (p1 ? (p2 ? 2 : 3) : (p2 ? 1 : 4)) : (p1 ? (p2 ? 4 : 1) : (p2 ? 3 : 2));

return num;
}

//返回该点距0点的距离
public function get length():number{
updata();
var num:number = math.sqrt(math.pow(_x,2) math.pow(_y,2));
return num;
}

//清除显示对象
public function clear():void{
dis = null;
}

//改变旋转值
public function set r(num:number):void{
_r = num;
if(dis != null) dis.rotation = num;
if(islisten) dispatchevent(new dotevent(dotevent.r_change,true));
}

//改变旋转值
public function get r():number{
updata();
return _r;
}

//改变x坐标
public function set x(num:number):void{
_x = num;
if(dis != null) dis.x = num;
if(islisten) dispatchevent(new dotevent(dotevent.x_change,true));
}

//设置x坐标
public function get x():number{
updata();
return _x;
}

//改变y坐标
public function set y(num:number):void{
_y = num;
if(dis != null) dis.y = num;
if(islisten) dispatchevent(new dotevent(dotevent.y_change,true));
}

//设置y坐标
public function get y():number{
updata();
return _y;
}
}
}