AS3自学笔记,慢慢补充

1、as原型机制使用 prototype 为原型类添加修改成员。

2、AS3的继承机制-原型继承

3、命名空间

namespace aaaa;
aaaa function bbbb(){}
 

用法

aaaa::bbbb();
 

或者(不推荐)

use namespace aaaa;
bbbb();

4、访问权限修饰符

类的访问权限
dynamic:动态的
final
internal:默认值,同包内
public

成员的访问权限:
internal
private:仅本类,子类不可见
protected:子类可见
public:任何位置可见
static:静态的,属于类本身,不属于任何实例
UserDefinedNamespace:用户自定义的命名空间

5、使用override关键字覆盖基类的getter、setter存取器

6、闭包方法

private function onLoaderComplete(i:int):Function {
	var fun:Function=function(e:Event) {
		imgArr[i]=e.target.content;  			                          
		if(i<urlArr.length-1) {  
			i++;  
			imgLoaded(i);  
		}else {
			dispatchEvent(new Event(LoadImg.COMPLETE));
		}
	};
	return fun;
}

7、super仅表示最近的父类。

8、多态(不曾忘记张老师的话:有继承有重写还有父类的引用指向子类的对象,这就是多态)

charCodeAt();      //获取某个字符的编码
indexOf();            //从左开始搜索
lastIndexOf();      //从右开始搜索
substr();              //第一个参数,起始位置;第二个参数,返回字符串的长度
substring();         //第一个参数,起始位置;第二个参数,字符串结尾的字符位置
slice();
split();                 //拆分字符串
replace();            //替换字符串 /定义基类class0
public class Class_0 {
	public function alert():void{
		Alert.show('class 0')
	}
}

//定义子类class1
public class Class_1 extends Class_0 {
	public override function alert():void{
		Alert.show('class 1')
	}
}

//定义子类class2
public class Class_2 extends Class_1 {
	public override function alert():void{
		Alert.show('class 2')
	}
}

//实现多态
var test_class1:Class_0 = new Class_1();
test_class1.alert();
//output: class 1

var test_class2:Class_1 = new Class_2();
test_class2.alert();
//output: class 2

var test_class3:Class_2 = new Class_2();
test_class3.alert();
//output: class 2

9、 dynamic类同时具有traits对象和hash表

当属性不能在traits对象中找到的时候就会检索hash表来寻找,这是动态类的特点!

10、使用 in 关键字进行成员检查。in亦可检查数组中是否包含指定索引的元素。

//成员检查

package com.tests {
	public dynamic class Person {
		public pname:String;
	}
}

import com.tests.Person;

var dali:Person = new Person();
trace("sb" in dali);	//false
dali.sb = "sb2";
trace("sb" in dali);	//true

dali.aaa = 'bbb';
var str:String = 'aaa';
trace(str in dali);		//true

//数组检查

var arr:Array = [12, 31, 34];
trace(2 in arr);		//true
trace(20 in arr);		//false
arr[20] = 14;
trace(20 in arr);		//true

11、delete成员删除(直接引入10中的Person类)

delete 不能删除非动态成员。
访问删除的后的属性将得到defined。
使用in关键字检查删除后的成员依然为true。
delete 删除不存在的成员返回false。

12、使用ByteArray深度复制数组。

 

private var testArr:Array = [1,3,5,7,9];
private var testArr2:Array;
private function copyArr():void{
	var byte:ByteArray = new ByteArray();
	byte.writeObject(testArr);
	byte.position = 0;
	testArr2 = byte.readObject();
	testArr[0] = 0;
	trace(testArr);
	trace(testArr2);
}

copyArr();
trace(testArr);          //0,3,5,7,9
trace(testArr2);    	//1,3,5,7,9

13、字符串操作

charCodeAt();      //获取某个字符的编码
indexOf();            //从左开始搜索
lastIndexOf();      //从右开始搜索
substr();              //第一个参数,起始位置;第二个参数,返回字符串的长度
substring();         //第一个参数,起始位置;第二个参数,字符串结尾的字符位置
slice();
split();                 //拆分字符串
replace();            //替换字符串

14、remove显示对象

使用removeChild()或者removeChildAt()方法后,只能将对象从显示容器中删除,要彻底删除内存占用还需再用delete才能彻底清除

15、Timer定时器

var timer:Timer = new Timer(2000,0);
timer.addEventListener("timer",function fun():void{
	//自定义触发函数
});
timer.start()

16、对象的旋转、斜切

<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" creationComplete="init();init2();init3();init4()">
	
	<!-- 旋转 -->
	<mx:Script>
		<![CDATA[
			private function init():void{
				//替换原有matrix对象
				var myMatrix:Matrix = img.transform.matrix;
				//计算图像的中心点
				var tempx:int, tempy:int;
				tempx = img.x + img.width/2;
				tempy = img.y + img.height/2;
				//启用定时器,连续旋转
				var timer:Timer = new Timer(1);
				timer.addEventListener("timer",function img_click():void{
					//将图像中心移动到坐标原点
					myMatrix.translate(-tempx, -tempy);
					//将对象逆时针旋转1度
					myMatrix.rotate(2 * Math.PI * (-1 / 360));
					//将图像中心移回图像原中心点
					myMatrix.translate(tempx, tempy);
					//应用变换到显示对象
					img.transform.matrix = myMatrix;
				});
				timer.start()
				//为图像添加点击事件
				img.addEventListener(MouseEvent.CLICK,function img_click():void{
					timer.running ? timer.stop() : timer.start();
				});
			}
		]]>
	</mx:Script>
	<mx:Panel width="100%" height="100%" title="对象旋转" backgroundColor="black">
		<mx:Canvas id="main_box" width="100%" height="100%">
			<!-- 两条辅线 HRule 和 VRule -->
			<mx:HRule y="{main_box.height/2}" width="100%"/>
			<mx:VRule x="{main_box.width/2}" height="100%"/>
			<mx:Image id="img" source="@Embed(source='assets/test.png')" 
				x="{main_box.width/2 - img.width/2}" 
				y="{main_box.height/2 - img.height/2}"/>
		</mx:Canvas>
	</mx:Panel>
	
	
	<!-- 斜切 -->
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private function init2():void{
				//替换原有matrix对象
				var myMatrix:Matrix = img2.transform.matrix;
				//临时变量,用于存储斜切大小和方向
				var temp:Number = 0;
				//标记变量,决定是向左、向右或 向上、向下
				var flag:Boolean = true;
				//启用定时器,连续斜切
				var timer:Timer = new Timer(5);
				timer.addEventListener("timer",function img2_click():void{
					//将对象按照水平向左的方向斜切,b为垂直方向
					if (temp >= 0.5){
						flag = true;
					}else if (temp <= -0.5){
						flag = false;
					}
					if (flag){
						temp -= 0.05;
					}else{
						temp += 0.05;
					}
					myMatrix.c = Math.tan(temp);
					//myMatrix.b = Math.tan(temp);
					//应用变换到显示对象
					img2.transform.matrix = myMatrix;
				});
				timer.start()
				//为图像添加点击事件
				img2.addEventListener(MouseEvent.CLICK,function img2_click():void{
					timer.running ? timer.stop() : timer.start();
				});
			}
		]]>
	</mx:Script>
	<mx:Panel width="100%" height="100%" title="斜切变形" backgroundColor="black">
		<mx:Canvas id="main_box2" width="100%" height="100%">
			<!-- 两条辅线 HRule 和 VRule -->
			<mx:HRule y="{main_box2.height/2}" width="100%"/>
			<mx:VRule x="{main_box2.width/2}" height="100%"/>
			<mx:Image id="img2" source="@Embed(source='assets/test.png')" 
				x="{main_box2.width/2 - img2.width/2}" 
				y="{main_box2.height/2 - img2.height/2}"/>
		</mx:Canvas>
	</mx:Panel>
	
	
	<!-- 斜切后旋转 -->
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private function init3():void{
				//替换原有matrix对象
				var myMatrix:Matrix = img3.transform.matrix;
				//计算图像的中心点
				var tempx:int, tempy:int;
				tempx = img3.x + img3.width/2;
				tempy = img3.y + img3.height/2;
				//将图像移到屏幕原点
				myMatrix.translate(-tempx, -tempy);
				//创建斜切matrix对象
				var skewMatrix:Matrix = new Matrix();
				skewMatrix.b = Math.tan(-0.5);
				skewMatrix.c = Math.tan(-0.5);
				//联合矩阵
				myMatrix.concat(skewMatrix);
				//将图像移回原中心点
				myMatrix.translate(tempx, tempy);
				//启用定时器,连续旋转
				var timer:Timer = new Timer(5);
				timer.addEventListener("timer",function img_click():void{
					//将图像中心移动到坐标原点
					myMatrix.translate(-tempx, -tempy);
					//将对象逆时针旋转1度
					myMatrix.rotate(2 * Math.PI * (-1 / 360));
					//将图像中心移回图像原中心点
					myMatrix.translate(tempx, tempy);
					//应用变换到显示对象
					img3.transform.matrix = myMatrix;
				});
				timer.start()
				//为图像添加点击事件
				img3.addEventListener(MouseEvent.CLICK,function img3_click():void{
					timer.running ? timer.stop() : timer.start();
				});
			}
		]]>
	</mx:Script>
	<mx:Panel width="100%" height="100%" title="斜切后旋转" backgroundColor="black">
		<mx:Canvas id="main_box3" width="100%" height="100%">
			<!-- 两条辅线 HRule 和 VRule -->
			<mx:HRule y="{main_box3.height/2}" width="100%"/>
			<mx:VRule x="{main_box3.width/2}" height="100%"/>
			<mx:Image id="img3" source="@Embed(source='assets/test.png')" 
				x="{main_box3.width/2 - img3.width/2}" 
				y="{main_box3.height/2 - img3.height/2}"/>
		</mx:Canvas>
	</mx:Panel>
	
	<!-- 缩放 -->
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private function init4():void{
				//替换原有matrix对象
				var myMatrix:Matrix = img4.transform.matrix;
				//计算图像的中心点
				var tempx:int, tempy:int;
				tempx = img4.x + img4.width/2;
				tempy = img4.y + img4.height/2;
				//临时变量,用户存储缩放大小
				var temp:Number = 0;
				//标记变量,决定放大或缩小
				var flag:Boolean = true;
				//启用定时器,连续缩放
				var timer:Timer = new Timer(500);
				timer.addEventListener("timer",function img4_click():void{
					//将图像移到屏幕原点
					myMatrix.translate(-tempx, -tempy);
					if (temp >= 0.9){
						flag = true;
					}else if (temp <= -0.9){
						flag = false;
					}
					if (flag){
						temp -= 0.01;
					}else{
						temp += 0.01;
					}
					myMatrix.scale(temp, temp);
					//myMatrix.b = Math.tan(temp);
					myMatrix.translate(tempx, tempy);
					//应用变换到显示对象
					img4.transform.matrix = myMatrix;
				});
				timer.start()
				//为图像添加点击事件
				img4.addEventListener(MouseEvent.CLICK,function img4_click():void{
					timer.running ? timer.stop() : timer.start();
				});
			}
		]]>
	</mx:Script>
	<mx:Panel width="100%" height="100%" title="对象缩放" backgroundColor="black">
		<mx:Canvas id="main_box4" width="100%" height="100%">
			<!-- 两条辅线 HRule 和 VRule -->
			<mx:HRule y="{main_box4.height/2}" width="100%"/>
			<mx:VRule x="{main_box4.width/2}" height="100%"/>
			<mx:Image id="img4" source="@Embed(source='assets/test.png')" 
				x="{main_box4.width/2 - img4.width/2}" 
				y="{main_box4.height/2 - img4.height/2}"/>
		</mx:Canvas>
	</mx:Panel>
	
</mx:HBox>

17、线性运动 

<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">
	<!-- 直线运动 -->
	<mx:Script>
		<![CDATA[
			[Bindable]private var offset:Number;
			private function init1():void{
				var point_arr:Array = new Array(3);
				offset = -hbox_01.width/2 + 2.5;
				point_arr[0] = new Point(150,50);
				point_arr[1] = new Point(250,450);
				point_arr[2] = new Point(50,450);
				var timer:Timer = new Timer(10,600);
				timer.addEventListener("timer",function fun():void{
					var flag:int = timer.currentCount;
					if (flag <= 200){
						hbox_01.x += ((point_arr[1].x - point_arr[0].x)/200);
						hbox_01.y += ((point_arr[1].y - point_arr[0].y)/200);
					}else if (flag > 200 && flag <=400){
						hbox_01.x += ((point_arr[2].x - point_arr[1].x)/200);
						hbox_01.y += ((point_arr[2].y - point_arr[1].y)/200);
					}else if (flag > 400 && flag <=600){
						hbox_01.x += ((point_arr[0].x - point_arr[2].x)/200);
						hbox_01.y += ((point_arr[0].y - point_arr[2].y)/200);
					}
				});
				timer.addEventListener(TimerEvent.TIMER_COMPLETE,function comp():void{
					timer.reset();
					timer.start();
				});
				timer.start();
			}
		]]>
	</mx:Script>
	<mx:Panel id="panel_01" title="直线运动" width="100%" height="100%" creationComplete="init1()" 
		verticalGap="10" horizontalAlign="center" verticalAlign="middle">
		<mx:Canvas width="100%" height="100%"> 
			<mx:HBox width="5" height="5" borderStyle="solid" cornerRadius="50" backgroundColor="black" x="150" y="50"/>
			<mx:HBox width="5" height="5" borderStyle="solid" cornerRadius="50" backgroundColor="black" x="250" y="450"/>
			<mx:HBox width="5" height="5" borderStyle="solid" cornerRadius="50" backgroundColor="black" x="50" y="450"/>
			<mx:HBox x="{150 + offset}" y="{50 + offset}" id="hbox_01" width="30" height="30" borderStyle="solid" cornerRadius="50" backgroundColor="black" backgroundAlpha="0.7"/>
		</mx:Canvas>
	</mx:Panel>
	
	<!-- 匀速直线运动(时长法) -->
	<mx:Script>
		<![CDATA[
			[Bindable]private var offset2:Number;
			private function init2():void{
				var point_arr:Array = new Array(3);
				offset2 = -hbox_02.width/2 + 2.5;
				point_arr[0] = new Point(150,50);
				point_arr[1] = new Point(250,450);
				point_arr[2] = new Point(50,450);
				var timer:Timer = new Timer(50,150);
				//计算每两点之间的距离
				var dist_a2b:Number = dist(point_arr[0],point_arr[1]);
				var dist_b2c:Number = dist(point_arr[1],point_arr[2]);
				var dist_c2a:Number = dist(point_arr[2],point_arr[0]);
				//运动总长
				var total_dist:Number = dist_a2b + dist_b2c + dist_c2a;
				//计算每两点之间运动一次的位移大小
				var rect_a2b:Number = Math.round(dist_a2b / total_dist * timer.repeatCount);
				var rect_b2c:Number = Math.round(dist_b2c / total_dist * timer.repeatCount);
				var rect_c2a:Number = timer.repeatCount - rect_a2b - rect_b2c;
				//var rect_c2a:Number = Math.round(dist_c2a / total_dist * timer.repeatCount);
				timer.addEventListener("timer",function fun():void{
					var flag:int = timer.currentCount;
					if (flag <= rect_a2b){
						hbox_02.x += ((point_arr[1].x - point_arr[0].x)/rect_a2b);
						hbox_02.y += ((point_arr[1].y - point_arr[0].y)/rect_a2b);
					}else if (flag > rect_a2b && flag <=rect_a2b + rect_b2c){
						hbox_02.x += ((point_arr[2].x - point_arr[1].x)/rect_b2c);
						hbox_02.y += ((point_arr[2].y - point_arr[1].y)/rect_b2c);
					}else if (flag > rect_a2b + rect_b2c && flag <= rect_a2b + rect_b2c + rect_c2a){
						hbox_02.x += ((point_arr[0].x - point_arr[2].x)/rect_c2a);
						hbox_02.y += ((point_arr[0].y - point_arr[2].y)/rect_c2a);
					}
				});
				timer.addEventListener(TimerEvent.TIMER_COMPLETE,function comp():void{
					hbox_02.x = 150 + offset2;
					hbox_02.y = 50 + offset2;
					timer.reset();
					timer.start();
				});
				timer.start();
				//计算两点之间的距离
				function dist(p1:Point, p2:Point):Number{
					return Math.sqrt(
						(
							Math.pow((p1.x-p2.x),2) + 
							Math.pow((p1.y-p2.y),2)
						)
					);
				}
			}
		]]>
	</mx:Script>
	<mx:Panel id="panel_02" title="匀速直线运动(时长法)" width="100%" height="100%" creationComplete="init2()" 
		verticalGap="10" horizontalAlign="center" verticalAlign="middle">
		<mx:Canvas width="100%" height="100%"> 
			<mx:HBox width="5" height="5" borderStyle="solid" cornerRadius="50" backgroundColor="black" x="150" y="50"/>
			<mx:HBox width="5" height="5" borderStyle="solid" cornerRadius="50" backgroundColor="black" x="250" y="450"/>
			<mx:HBox width="5" height="5" borderStyle="solid" cornerRadius="50" backgroundColor="black" x="50" y="450"/>
			<mx:HBox x="{150 + offset2}" y="{50 + offset2}" id="hbox_02" width="30" height="30" borderStyle="solid" cornerRadius="50" backgroundColor="black" backgroundAlpha="0.7"/>
		</mx:Canvas>
	</mx:Panel>
	
	<!-- 匀速直线运动(速度法) -->
	<mx:Script>
		<![CDATA[
			[Bindable]private var offset3:Number;
			private function init3():void{
				var timer:Timer = new Timer(50,150);
				//创建三个point对象
				var point_arr:Array = new Array(3);
				offset3 = -hbox_03.width/2 + 2.5;
				point_arr[0] = new Point(150,50);
				point_arr[1] = new Point(250,450);
				point_arr[2] = new Point(50,450);
				//计算每两点之间的距离
				var arr_dist:Array = new Array(4);
				arr_dist[1] = dist(point_arr[0],point_arr[1]);
				arr_dist[2] = dist(point_arr[1],point_arr[2]);
				arr_dist[3] = dist(point_arr[2],point_arr[0]);
				//运动总长
				arr_dist[0] = arr_dist[1] + arr_dist[2] + arr_dist[3];
				//计算移动速度
				var speed:Number = arr_dist[0]/timer.repeatCount;
				//已过行程
				var past:Number = 0;
				//每阶段x轴和y轴位移数组
				var setpx:Array = new Array(3);
				var setpy:Array = new Array(3);
				//第一阶段x,y位移大小
				setpx[0] = (point_arr[1].x - point_arr[0].x)/arr_dist[1]*speed;
				setpy[0] = (point_arr[1].y - point_arr[0].y)/arr_dist[1]*speed;
				//第二阶段x,y位移大小
				setpx[1] = (point_arr[2].x - point_arr[1].x)/arr_dist[2]*speed;
				setpy[1] = (point_arr[2].y - point_arr[1].y)/arr_dist[2]*speed;
				//第三阶段x,y位移大小
				setpx[2] = (point_arr[0].x - point_arr[2].x)/arr_dist[3]*speed;
				setpy[2] = (point_arr[0].y - point_arr[2].y)/arr_dist[3]*speed;
				timer.addEventListener("timer",function fun():void{
					var r:Number;
					if (past <= arr_dist[1]){
						r = 0;
					}else if (past > arr_dist[1] && past <= (arr_dist[1] + arr_dist[2])){
						r = 1;
					}else if (past > (arr_dist[1] + arr_dist[2]) && past <= arr_dist[0]){
						r = 2;
					}
					hbox_03.x += setpx[r];
					hbox_03.y += setpy[r];
					past += speed;
				});
				timer.addEventListener(TimerEvent.TIMER_COMPLETE,function comp():void{
					hbox_03.x = 150 + offset3;
					hbox_03.y = 50 + offset3;
					timer.reset();
					past = 0;
					timer.start();
				});
				timer.start();
				//计算两点之间的距离
				function dist(p1:Point, p2:Point):Number{
					return Math.sqrt(
						(
							Math.pow((p1.x-p2.x),2) + 
							Math.pow((p1.y-p2.y),2)
						)
					);
				}
			}
		]]>
	</mx:Script>
	<mx:Panel id="panel_03" title="匀速直线运动(速度法)" width="100%" height="100%" creationComplete="init3()" 
		verticalGap="10" horizontalAlign="center" verticalAlign="middle">
		<mx:Canvas width="100%" height="100%"> 
			<mx:HBox width="5" height="5" borderStyle="solid" cornerRadius="50" backgroundColor="black" x="150" y="50"/>
			<mx:HBox width="5" height="5" borderStyle="solid" cornerRadius="50" backgroundColor="black" x="250" y="450"/>
			<mx:HBox width="5" height="5" borderStyle="solid" cornerRadius="50" backgroundColor="black" x="50" y="450"/>
			<mx:HBox x="{150 + offset3}" y="{50 + offset3}" id="hbox_03" width="30" height="30" borderStyle="solid" cornerRadius="50" backgroundColor="black" backgroundAlpha="0.7"/>
		</mx:Canvas>
	</mx:Panel>
</mx:HBox>

18、矩阵运算

点A旋转S角度后B点坐标计算:

 如图所示

 计算步骤:
OA = a/cos(β) = b/sin(β)
cos(s + β) = m / OA 
sin(s + β) = n / OA
m = OA  * cos(s + β) = OA * (cos(s)*cos(β) - sin(s)*sin(β)) = a*cos(s) -  b*sin(s)
n = OA  * sin(s + β) = OA * (sin(s)*cos(β) + cos(s)*sin(β)) = a*sin(s) + b*cos(s)
 

 

 

 

 

 

 

 

 

你可能感兴趣的:(prototype)