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
相关文档:
import flash.net.navigateToURL;
import flash.net.URLRequest;
//把链接强制转换成URLRequest方式,这样才可以被navigateToURL连接
var request:URLRequest = new URLRequest("aaa.html"); //跳转的页面
navigateToURL(request,"_self");//跳转方式
stop(); ......
在flex开发环境中编写as3代码是很方便的,借助flex开发环境代码都有自动提示功能,但是在做flash游戏的时候,在涉及到对话框的时候,我遇到一个很棘手的问题,就是在美工用flash cs布局好了的界面,我在界面上命名了实例(如textfield类型 var nameText:String),在导出这个类(guestDialog)的时候(这个类继承自MovieClip ......
目前来讲,我依然还不会做一个小游戏出来。做游戏需要知道很多东西,这个flash 只是一个自娱自乐的东西。要是看了误导就别怪了。
好,制作这个东西之前,其实这篇文章有一些重复了,不过只是一种demo式的演示。通过键盘来控制控制一个人物走动。
上面是一张透明的png位图。有四个不同的方向。可以看到 基本上上下 ......