博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ActionScript 3.0 记要(1): 基本语法
阅读量:5768 次
发布时间:2019-06-18

本文共 7314 字,大约阅读时间需要 24 分钟。

  hot3.png

近期要做的东西既需要运行于网络又需要运行于本地, 并要尽量多地兼容各种平台(包括手机平台).
大概了解了 PhoneGap、HTML5、Adobe AIR、Flash、SilverLight、uniGUI、Qt, 发现还是老牌的 Flash 更适合需求.
因需全部用代码完成界面与逻辑, 所以主要的学习任务是 ActionScript 3.0;
感谢 Delphi 给我的营养, 让我不再畏惧其它工具和语言.
对 ActionScript 3.0 的初步感受: 很像且优于 JavaScript, 特别在面向对象方面; 另外参考资料也特别充足, 估计两三天即可修完需要的部分.

测试环境 Flash CS5; 测试准备:
1、新建 ActionScript 3.0 工程;
2、在第一帧上点击右键 -> 动作, 打开代码输入窗口;
3、输入代码后, Ctrl+Enter 运行.

测试输出:

trace(123);trace("abc");

变量与常量:

var a:int, b:int, c:int;a = b = c = 6;trace(a, b, c); //6 6 6const d:int = 123;trace(d);

数据类型:

//整形var iV1:int = -1;var iV2:int = new int(-2);var iV3:int = new int("-3");trace(iV1);trace(iV2);trace(iV3);//无符号整形var uV1:uint = 1;var uV2:uint = new uint(2);trace(uV1);trace(uV2);//浮点数var nV1:Number = 3.14159265;var nV2:Number = new Number(-3.14);trace(nV1);trace(nV2);//布尔值var bV1:Boolean = true;var bV2:Boolean = new Boolean(false);trace(bV1);trace(bV2);//字符串var sV1:String = 'ActionScript';var sV2:String = new String("万一的 Delphi 博客");trace(sV1);trace(sV2);/* Object *///Datevar dateVal:Date = new Date(2011, 5, 13);trace(dateVal.toString());//数组var sArr1:Array = ["spring", "summer", "autumn", "winter"];var sArr2:Array = new Array("spring", "summer", "autumn", "winter");trace(sArr1[0]);trace(sArr2[1]);var iArr = [11, 22, 33];trace(iArr[iArr.length-1]);//XMLvar xmlVal:XML = 
AAA
BBB
trace(xmlVal.item[0]); //AAA

数据类型默认值:

//整形var i:int;trace(i); //0trace(int.MIN_VALUE, int.MAX_VALUE); //-2147483648 2147483647//无符号整形var u:uint;trace(u); //0trace(uint.MIN_VALUE, uint.MAX_VALUE); //0 4294967295//浮点数var n:Number;trace(n); //NaNtrace(Number.MIN_VALUE, Number.MAX_VALUE); //4.9406564584124654e-324 1.79769313486231e+308//布尔值var b:Boolean;trace(b); //false//字符串var s:String;trace(s); //null//Objectvar obj:Object;trace(obj); //nullvar dt:Date;trace(dt); //null//未声明var arr:Array = [11, 22];trace(arr[1]); //22trace(arr[2]); //undefinedvar val;trace(val);  //undefinedtrace(void); //undefined

类型转换:

//var i:int;i = int(true);  trace(i); //1i = int(false); trace(i); //0i = int(5.67);  trace(i); //5i = int(0x1A);  trace(i); //26i = parseInt('3'); trace(i); //3i = int("4");      trace(i); //4//var n:Number;n = parseFloat("3.14159265"); trace(n); //3.14159265n = Number("3.14159265");    trace(n); //3.14159265//var b:Boolean;b = Boolean(-1);   trace(b); //trueb = Boolean(0);    trace(b); //falseb = Boolean(1);    trace(b); //trueb = Boolean(2);    trace(b); //trueb = Boolean("");   trace(b); //falseb = Boolean(" ");  trace(b); //truevar obj1:Object;var obj2:Object = new Object();b = Boolean(obj1);   trace(b); //falseb = Boolean(obj2);   trace(b); //true//var str:String;var arr:Array = ["primary", "secondary", "tertiary"];str = String(arr); trace(str);     //primary,secondary,tertiarystr = String(Math.PI); trace(str); //3.141592653589793

运算符:

[]       //初始化数组{x:y}    //初始化对象()       //对表达式进行分组f(x)     //调用函数new      //调用构造函数x.y x[y] //访问属性<>    //初始化 XMLList 对象 (E4X)@        //访问属性 (E4X)::       //限定名称 (E4X)..       //访问子级 XML 元素 (E4X)++       //递增(前缀、后缀)--       //递减(前缀、后缀)+        //一元 +-        //一元 - (非)!        //逻辑 NOT~        //按位 NOTdelete   //删除属性typeof   //返回类型信息void     //返回未定义值*        //乘/        //除%        //求模+        //加-        //减<<       //按位左移位>>       //按位右移位>>>      //按位无符号右移位<        //小于>        //大于<=       //小于或等于>=       //大于或等于as       //检查数据类型in       //检查对象属性instanceof //检查原型链is       //检查数据类型==       //等于!=       //不等于===      //全等!==      //不全等&        //按位 AND^        //按位 XOR|        //按位 OR&&       //逻辑 AND||       //逻辑 OR?:       //三目运算符=        //赋值*=       //乘法赋值/=       //除法赋值%=       //求模赋值+=       //加法赋值-=       //减法赋值<<=      //按位左移位赋值>>=      //按位右移位赋值>>>=     //按位无符号右移位赋值&=       //按位 AND 赋值^=       //按位 XOR 赋值|=       //按位 OR 赋值

语句:

//if 语句if () { } else { }//三目var a:int, b:int, c:int;a = 1;b = 2;c = (a > b) ? a : b;trace(c); //2//switch 语句var dt:Date = new Date();var day:uint = dt.getDay();switch(day){    case 0:    trace("Sunday");    break;    case 1:    trace("Monday");    break;    case 2:    trace("Tuesday");    break;    case 3:    trace("Wednesday");    break;    case 4:    trace("Thursday");    break;    case 5:    trace("Friday");    break;    case 6:    trace("Saturday");    break;    default:    trace("Out of range");    break;}//for 循环for (var i:int = 0; i < 5; i++){    trace(i);}//for in 循环var arr:Array = ["one", "two", "three"];for (var s:String in arr){    trace(s, ":", arr[s]);}/***********0 : one1 : two2 : three************/var obj:Object = {x:20, y:30};for (var s:String in obj){    trace(s, ":", obj[s]);}/***********y : 30x : 20************///for each 循环var arr:Array = ["one", "two", "three"];for each (var str in arr){    trace(str); //one/two/three}var obj:Object = {x:20, y:30};for each (var num in obj){    trace(num); //20/30}var myXML:XML = 
Jane
Susan
John
;for each (var item in myXML.fname){ trace(item); //Jane/Susan/John}//while 循环与 do while 循环var i:int;i = 0;while (i<5){ trace(i); //0/1/2/3/4 i++;}i = 5;do{ trace(i); //5 i++;} while(i<5);

函数:

//函数语句function Add(a:int, b:int):int{    return a + b;}trace(Add(1, 2)); //3//----------------------------------------------------------------//函数表达式var traceParameter:Function = function (str:String){    trace("Hello " + str);}; //分号traceParameter("ActionScript"); //Hello ActionScript//----------------------------------------------------------------//函数成员function Sum(a:int, b:int):int { return a+b; }var arr:Array = new Array();arr[0] = Sum;arr[1] = function(a:int, b:int) { return a*b; };trace(arr[0](3, 4)); //7trace(arr[1](3, 4)); //12//----------------------------------------------------------------//嵌套函数function myFun():String{    function Fun1():String { return "Hello"; }    function Fun2():String { return "ActionScript"; }    return Fun1() + " " + Fun2();}trace(myFun()); //Hello ActionScript//----------------------------------------------------------------//默认参数function Sum(a:int, b:int=2, c:int=3):int { return a+b+c; }trace(Sum(1)); //6//----------------------------------------------------------------//基本数据类型参数(Boolean、Number、int、uint、String)按值传递function Inc1(a:int):void {    a++; }var b:int = 1;Inc1(b);trace(b); //1//对象参数按引用传递function Inc2(aObj:Object):void{    aObj.x++;    aObj.y++;}var obj:Object = { x:1, y:2 };Inc2(obj);trace(obj.x, obj.y); //2 3//----------------------------------------------------------------//函数中的 arguments 对象function myFun(a:int, b:int, c:int):void{    for (var i:uint = 0; i< arguments.length; i++)    {        trace(arguments[i]);    }}myFun(11, 22, 33); //11/22/33//arguments.callee 表示函数本身; 使用回调完成阶乘的例子:var factorial:Function = function (x:uint){    if(x == 0)    {        return 1;    }    else    {        return (x * arguments.callee(x - 1)); //同 return (x * factorial(x - 1));    }}trace(factorial(5)); //120//----------------------------------------------------------------//... (rest) 数组参数:function myFun(...myArgs):void{    for (var i:uint = 0; i < myArgs.length; i++)    {        trace(myArgs[i]);    }}myFun(1, 2, 3); //1/2/3myFun("A", "B", "C", 1, 2, 3); //A/B/C/1/2/3

错误处理:

try {    throw new Error("抛出错误");    trace("不会执行");} catch(e:Error){    trace(e.message);}finally {    trace("是否发生错误都会执行");}

转载于:https://my.oschina.net/hermer/blog/319653

你可能感兴趣的文章
认知计算 Cognitive Computing
查看>>
左手坐标系和右手坐标系 ZZ
查看>>
陀螺仪主要性能指标
查看>>
Java 架构师眼中的 HTTP 协议
查看>>
Linux 目录结构和常用命令
查看>>
Linux内存管理之mmap详解 (可用于android底层内存调试)
查看>>
利润表(年末)未分配利润公式备份
查看>>
Android开发中ViewStub的应用方法
查看>>
gen already exists but is not a source folder. Convert to a source folder or rename it 的解决办法...
查看>>
HDOJ-2069Coin Change(母函数加强)
查看>>
遍历Map的四种方法
查看>>
JAVA学习:maven开发环境快速搭建
查看>>
Altium Designer 小记
查看>>
【Linux高级驱动】I2C驱动框架分析
查看>>
赵雅智:js知识点汇总
查看>>
二维有序数组查找数字
查看>>
20个Linux服务器性能调优技巧
查看>>
多重影分身:一套代码如何生成多个小程序?
查看>>
Oracle将NetBeans交给了Apache基金会
查看>>
填坑记:Uncaught RangeError: Maximum call stack size exceeded
查看>>