// declare and set initial variables and properties
with (puck) {
initx = _x;
inity = _y;
top = _y-97;
bottom = _y+97;
left = _x-97;
right = _x+97;
}
//
_root.onEnterFrame = function() {
with (puck) {
// make circle continuously rotate
_rotation += 10;
//
// calculate distance from starting point
delta_x = _x-initx;
delta_y = _y-Inity;
distance = Math.sqrt((delta_x*delta_x)+(delta_y*delta_y));
}
//
// calculate the angle from the starting point in radians
radians = Math.atan2(delta_y, delta_x);
if (delta_y<0) {
radians += (2*Math.PI);
}
//
// convert radians to degrees
degrees = Math.floor(radians/(Math.PI/180));
};
//
// make puck draggable
puck.onPress = function() {
startDrag(puck, false, left, top, right, bottom);
};
puck.onRelease = function() {
stopDrag();
};
//
// reset button
reset.onRelease = function() {
puck._x = initx;
puck._y = inity;
};
---------------------------------------------------------------
// declare and set initial variables and properties
with (puck) {
initx = _x;
inity = _y;
top = _y-97;
bottom = _y+97;
left = _x-97;
right = _x+97;
}
//上段代码初始化变量.
_root.onEnterFrame = function() {
with (puck) {
// make circle continuously rotate
//通过onEnterFrame循环使圆不断转动.
_rotation += 10;
//
// calculate distance from starting point
//计算puck与起始点的距离.
delta_x = _x-initx;
delta_y = _y-Inity;
distance = Math.sqrt((delta_x*delta_x)+(delta_y*delta_y));
}
//
// calculate the angle from the starting point in radians
//计算从起始开始转动的角度以弧度的方式.
radians = Math.atan2(delta_y, delta_x);
if (delta_y<0) {
radians += (2*Math.PI);
}
//
// convert radians to degrees
//将弧度转换为角度.
degrees = Math.floor(radians/(Math.PI/180));
};
//
// make puck draggable
//使PUCK能够被拖动.
puck.onPress = function() {
startDrag(puck, false, left, top, right, bottom);
};
puck.onRelease = function() {
stopDrag();
};
//
// reset button
//重置按钮,使PUCK回到初始位置.
reset.onRelease = function() {
puck._x = initx;
puck._y = inity;
};
---------------------------------------------------------------
// 声明并设置变量的初始值和属性(关键是从相对坐标这角度出发去看这个程序)
// initx=_x和inity=_y这两句语句得到了目前没移动puck之前的puck的X轴Y轴
// 坐标,下面的delta_x和delta_y的数值其实都是相对于这个坐标,理解了这一点的话,
// 这程序就比较好明白了。
// 通过这样的设置,使initx,inity,top,bottom,left,right这6个变量拥有唯
// 一一个数值,这些数值是固定不变的,不要将它和后面的_x,_y混为一谈;
// 程序一开始运行时已经把这几个变量的值给设定好了,所以程序最后那3行语句
// 中要将中间那个旋转的圆恢复到初始位置时只需要将它目前的X坐标值和Y坐标值
// 等于初始的X、Y坐标值就行了,也就是说分别等于initx和inity.
// 实际上可以这样来理解:无论圆(puck)放在场景的什么地方,initx和inity都
// 等于0,top,bottom,left,right分别等于-97,97,-97,97,从相对坐标的角度来看,
// 实际上也是这样,因为
// 从一开始已经限定死圆(puck)上下左右最大能够到达的范围
// 而为什么puck上下左右能够到达的范围刚好到达(等于)影片剪辑"crosshair"上下
// 左右的最大值呢?其实这很简单,由initx=_x,inity=_y都等于0得出无论puck放在场景的
// 任何地方,一开始都是以puck本身所在的地点为0坐标,此时原始场景的坐标对puck
// 无任何作用;确定这一点后,我们再看,puck本身的尺寸为:21.1x21.1,而"crosshair"的
// 尺寸为:216x216,将puck和crosshair平均分配到坐标轴的4个方向,看看又怎样的结果?
// 没错了,puck上下左右各个方向的长度都为10.55(大约等于11),crosshair上下左右各个方向
// 的长度都为:108,用108-11=97,也就是说puck向各个方向所能移动的最大距离只能为97
with (puck) {
initx = _x;
inity = _y;
top = _y-97;
bottom = _y+97;
left = _x-97;
right = _x+97;
}
//
_root.onEnterFrame = function() {
with (puck) {
// 使圆连续地旋转
_rotation += 10;
//
// 计算目前点到起始点之间的距离;其中Math.sqrt(x)语句的意思为:
// 计算并返回指定数字的平方根,x为一个大于或等于0的数字或表达式
delta_x = _x-initx;
delta_y = _y-Inity;
distance = Math.sqrt((delta_x*delta_x)+(delta_y*delta_y));
}
//
// 计算并且用弧度表示目前点与起始点之间所构成的角度,其中Math.atan2
// 意思为:以弧度为单位计算并返回 y/x 的反正切值。返回值表示相对直角
// 三角形对角的角,x 是临边边长,而 y 是对边边长。
radians = Math.atan2(delta_y, delta_x);
if (delta_y<0) {
radians += (2*Math.PI);
// Math.PI的意思为:代表一个圆的周长与其直径的比值的数学常数,
// 表示为pi,其值大约为 3.14159265358979。
}
// 将弧度转换成角度,Math.floor的意思为:返回参数 x 中指定的数字
// 或表达式的下限值。下限值是小于等于指定数字或表达式的最接近的整数
// 下面的代码返回一个值 12:
// Math.floor(12.5);
degrees = Math.floor(radians/(Math.PI/180));
};
//
// 设置 puck 令其可以用鼠标左键按住移动
puck.onPress = function() {
startDrag(puck, false, left, top, right, bottom);
};
puck.onRelease = function() {
stopDrag();
};
//
// 给实例名为 reset 的按钮设置参数,用户可以通过单击这个
// 按钮让面板中的各个文字框中的数值恢复到初始值;
reset.onRelease = function() {
puck._x = initx;
puck._y = inity;
};
---------------------------------------------------------------
补充一下,整个程序是围绕一个puck的对象操作的,你要学会如何去操作一个对象所具有的全部属性和方法,以及在一个已有对象,添加其属性并继承其原有的属性和方法.
例子,不能看它去完成的具体的事务,而是学习它的编程方法和结构,我觉得AS比较侧重于面向对象的编程方式和思维,和JAVA有很多相同之处.