Flash Tip: Bracket [] Syntax
It's been a while since I've posted any Flash tips. I recon it's
time that changed and I get back to spreading some of that knowledge that's
lofting up there in that big empty space I call a brain. This particular tip
comes from a recent post on the ActionScript.org forums
.
Brackets evaluates the string it contains and resolves a
variable reference for the object preceding the first [. So, for example,
_level0["Rect"]; is the same as _level0.Rect; _level0["Rect"+1]; is the same as
_level0.Rect1;
Arrays rely on this method of variable resolution since
array values are stored in variables whose names are numbers. Normally numbers
are not allowed as variable names because dot syntax cannot properly resolve
variables names starting with numbers as it assumes it to be a number value,
i.e. _level0.3something; screws up because Flash gets to the 3 and thinks the
number 3, not a variable name starting with 3. However, this naming restriction
can be averted using bracket syntax, _level0["3something"]; and in arrays'
cases, is required, someArray[3]. Note that bracket contents do not have to be
strings, though a string representation of the contents is used to resolve the
variable--this through the toString() method.
All variable types in Flash
have a toString method which determines what its value looks like as a string.
For numbers, its just a value conversion of number to string, visually not
really changing at all. Arrays show a string listing of their values separated
by commas and for basic objects, you've probably seen its default as "[object
Object]" in a trace in Flash. Bracket syntax uses this string representation for
evaluation so someArray[3];
is seen as someArray[(3).toString()];
or someArray["3"];
You can use this to your advantage with
other variable types like generic objects. See the following:
myNameObject = new Object();
myNameObject.toString =
func
相关文档:
在当今数字技术飞速发展的时代, Flash 因其非易失性及可擦除性而在数码相机、手机、个人数字助理(PDA )、掌上电脑、MP3 播放器等手持设备中得到广泛的使用。Nor 和Nand 是两种主要的非易失闪存技术。自1989 年东芝公司发表了Nand Flash 结构以来,Nand Flash 以其具有更小的体积, 更快的写入和擦除速度, 更多次的可擦除次数, ......
在flex开发环境中编写as3代码是很方便的,借助flex开发环境代码都有自动提示功能,但是在做flash游戏的时候,在涉及到对话框的时候,我遇到一个很棘手的问题,就是在美工用flash cs布局好了的界面,我在界面上命名了实例(如textfield类型 var nameText:String),在导出这个类(guestDialog)的时候(这个类继承自MovieClip ......
2010即将来临,adobe将会迎来一个重要的一年,Flash 平台技术的构建,将会让更多学习Flash技术的人得到受惠。学习这个平台技术,就需要我们知道该技术是怎样一回事?学习flash 编程的人会了解到, adobe 很多编程相关的产品都在构建在 as 身上,把这门语言作为一个沟通的桥梁,这是一大特色。很多人质疑a ......
用FLASH做RPG,我晕,呵呵,听到这个,很多人恐怕要这么认为。不过FLASH强大的脚本功能和出色的动画函数,给我FLASH做RPG游戏提供了
很好的支持。从今天开始大愚分2节课程给大家讲述做RPG游戏的基础模型。由于大愚也是一个菜鸟,所以错误之处,还请见谅。
由于FLASH还不能支持3D,所以在FLASH的游戏中 ......