论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Flash AS教程
Tag:2.0,3.0菜鸟,游戏,,cs,技巧,源码,,文本,文字,函数,音乐,随机,拖拽,asp,access,xml,mc,视频教程

学习【从Flash as2到Flash as3】中常量&操作符&参数间的差异

文章类别:Flash AS | 发表日期:2009-8-2 17:49:43

学习【从Flash as2到Flash as3】中常量&操作符&参数间的差异

随着Flash CS3正式版的发布,估计很多朋友都开始从Flash as2迁移到Flash as3了。不过Flash as3比Flash as2改变的实在太多,在Flash as2中很多方法属性在Flash as3中并不相同,甚至消失,而且目前帮助文档是E文的,现下的Flash as3基础文章也并不多,因此特开此贴,集合一些基础性的要点,尽量以例子来说明,让大家更快更好的往Flash as3迁移。文中如有错误,敬请指正。有兴趣的朋友也可以一起来添加。

1、常量
先看Flash as2代码:
复制内容到剪贴板
代码:
var str:String;
var num:Number;
var boo:Boolean;
var obj:Object;
var notyped;
trace(str+newline+num+newline+boo+newline+obj+newline+notyped);
//return
undefined
undefined
undefined
undefined
undefined
再看AS3代码:
复制内容到剪贴板
代码:
var str:String;
var num:Number;
var boo:Boolean;
var obj:Object;
var notyped;
trace(str+"\n"+num+"\n"+boo+"\n"+obj+"\n"+notyped);
//return
null
NaN
false
null
undefined
从上面2个例子可以清楚的看到:在Flash as2中String、Number、Boolean、Object的默认值都是undefined,而在Flash as3中则分别为null、NaN、false、null。只有未作申明的变量的默认值才是undefined。因此,在Flash as2中一个"undefined"打天下的时代已经过时了,在Flash as3中更加明确。同时Flash as2中还有一个常量newline在Flash as3中已经取消,由"\n"替换。另外特别说明就是trace方法的功能也加强了,参数个数不限,因此上面可以改成trace(str,num,boo,obj,notyped)了。

2、操作符
这里只讨论instanceof。虽然Flash as3中还保留了这个操作符,但Flash as3中推荐用is代替它。而且is的用法更为灵活。
代码:
复制内容到剪贴板
代码:
var mystr:String="test";
var mytest:Test = new Test();//Test is extends SuperTest
trace(mytest instanceof SuperTest); //AS2 return false //AS3 return true
trace(mystr instanceof String);//AS2 return false
trace(mystr instanceof String);//AS3 return true
trace(mystr is String);//AS3 return true
从此例可以看出instanceof在Flash as3中的比Flash as2中有了变化。在Flash as2中instanceof不会将原始类型转换为包装对象,因此在上面例子中的String验证中返回false,并且它对超类(如SuperTest)不起作用。而在Flash as3中它只检查原型链,所以在第一个trace中会返回true。但是Flash as3中instanceof并不支持接口,而is支持。
代码:
复制内容到剪贴板
代码:
var mytest:Test = new Test();//Test implements InterfaceTest
trace(mytest is InterfaceTest); //AS3 return true
trace(mytest instanceof InterfaceTest); //AS3 return false
至于add、eq、gt、ge、<>、and、not、or、ne、lt、le等AS1的语法在as3中统统取消了。

3、参数
在Flash as3中增加了一个... (rest)这样的参数。它的作用是指明函数可以接收任意多个以逗号分隔的参数。Flash as3代码:
复制内容到剪贴板
代码:
function testfun1(param0, param1, ...arg)
{
    trace(arg is Array,arg,arg.length);
}
testfun1("param0","param1","param2","param3");
//return: true param2,param3 2
在这里...arg表示一个名为arg的数组。当运行testfun1后,arg=["param2","param3"]。还记得function有个arguments类么,当使用了...(rest)后,argumnets就无法获取了。但是如上例所示arg.length同样可以使用。但是并没有类似arguments.callee的这种方法,所以要使用...arg的时候确保不会使用arguments.callee的方法。

视频教程列表
文章教程搜索
 
Flash AS推荐教程
Flash AS热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058