主题:  Lingo和ActionScript之比较(有翻译)

Call Me Simon

职务:普通成员
等级:2
金币:2.0
发贴:646
#12001/3/16 19:38:21
Comparing Lingo and ActionScript, Part I

by Gary Rosenzweig

There's a lot of competition and confusion between Director and Flash these days. They are both made by Macromedia, they both produce movies, and when these movies are played in a Web browser, it is sometimes called Shockwave for both. Some Director developers are jumping ship and starting to use Flash. Others, like myself, are using both tools. I thought it would be a good idea to write a series of columns comparing the scripting languages of both Director and Flash. This will be useful for a programmer that already knows one, but not the other. It will be interesting, I hope, for others as well.

Where Did They Come From?
Lingo is a very old language compared to more recent languages like Java. It was created at Macromedia (Macromind) by John Thompson. To me, it looks like it has its roots in Hypercard's scripting language, which was Director's biggest competitor back in the early Mac days. I'd guess, then, that its origins are really in BASIC, which was devised as a language that read more like English than symbols and numbers.

The most important thing about Lingo is that the language has evolved in leaps and bounds since its origins. Other languages like BASIC, Pascal and C, start off pretty close to full-featured and grow only in small steps to handle new hardware. Lingo, on the other hand, has grown and changed so much that it looks like a completely different language than it was in Director 4.

ActionScript is a much newer language. You might say that it only came into existence with the release of Flash 5 last year. Sure Flash 4 had a scripting language in it, but it was written with drop-down menus and fill-in-the-blanks dialogs. It looks very different than the ActionScript in Flash 5.

ActionScript supposedly has its origins in javascript. However, I find it to be closer to Java or C++. I think the javascript comparison is just harmless Macromedia marketing hype.

Where Do They Go?
In Director, scripts are usually their own cast members. Those members can be placed on a frame in the Score, or on a sprite on the Stage. Alternatively, you can have scripts that are part of a specific cast member.

Since most scripts exist as their own cast member, you can attach the same script member to multiple frames or sprites. It is usually called a behavior. The great advantage to behaviors is that you can write one behavior script and attach it to a hundred sprites. Then, one change to that script will affect all of those sprites. At the same time, each sprite will retain its own values for the behavior properties. This is true object-oriented programming.

In Flash, there are no separate library elements for scripts. Instead, scripts are specifically attached to a frame in the timeline, or a movie clip on the Stage. This means that a script exists in one, and only one, place. So if you attach a script to a movie clip on the Stage, the script only exists there. If you want to put the same script on another movie clip, you can copy and paste it, but they will be two separate copies of the script text. This prevents ActionScript from being as object-oriented as I would like, but you can get around it by using simple, one-line movie clip scripts that call the same function at the root level.

Global and Local Variables
It is often said that if you want to know how to do something in Flash, think about how you would do it in Director and do the exact opposite. This is true for variables. Lingo assumes that all variables are local unless they are declared global thus:

global gMyGlobal

ActionScript, on the other hand, assumes all variables are global, unless you declare them to be local variables with the var declaration.

var myLocalVar = 0;

I usually just let all of the variables in Flash stay global. This helps me program faster, but all creates problems like when you call a function containing a variable name that is the same as one you are using for something else.

Flash also works on levels. The root level is the main timeline, but any movie clip can create a whole new level under that. A global variable is only global in its own level. So a movie clip's global variables will not interfere with the main timeline's global variables.

Comments
In Lingo, you would use a double-dash (--) to define that the rest of a line of code is a comment. In ActionScript, it is a double-slash (//) that is used to accomplish the same thing.

Message and Output Windows
In Director, we have the Message window. Any use of the command put without an into, after or before modifier will result in something being placed in the Message window. This is useful for debugging as well as making quick results-oriented programs.

Flash has much the same thing in the Output window. Instead of put, you'd use the trace command to place a single value in the Output window.

The Output window differs from the Message window in that you cannot type anything into the Output window. It is for output only, as the name implies.

Also the trace command can only take a single item, unlike the put command which can take multiple parameters, separated by commas. To get more than one item into the Output window on one line, you'd have to use string concatenators. trace (a + " " + b);

Comparisons
Lingo uses the equals symbol (=) for both variable assignment and comparisons. For instance, you can assign the variable a the value of 5 with this line:

a = 5

You could also test if a is equal to 5 with this line:

if a = 5 then

ActionScript uses the double-equals sign (==) for comparisons. This is very much like C, C++ or Java.

if (a == 5)

Notice that ActionScript also requires parenthesis around a comparison. It gets quite upset if you don't do that.

In Lingo, you can use <> to see if something is not equal to something else. ActionScript uses the != symbol for same task. The exclamation point is used in ActionScript in the same way the word not is used in Lingo. Other symbols, like < and > are the same between the two languages.

Incrementers and Such
Lingo really has no ability to use what are called incrementers. For instance, if you wanted to add 1 to the variable a in Lingo, you'd have to do this:

a = a + 1

This is annoying for anyone that has ever programmed in C, C++ or Java. In ActionScript, you can do the much more sensible statement:

a++;

The ++ denotes that one is to be added to the variable. You can also do this to add more than one:

a += 5;

This would add 5 to a. You can use ++, --, += or -=. Multiplication and division work as well.

In addition to syntax like ++ being easier to type, you can also use them inside of other pieces of code. For instance, this line will print the value of a to the Output window, and add one to it right after the trace command is complete.

trace (a++);

If you want a to be incremented before the trace command was executed, all you need to do is place the ++ before the a instead of after it.

trace (++a);

编辑历史:[这消息被flyingbird编辑过(编辑时间2001-03-24 09:11:42)]


koala_5d

职务:普通成员
等级:2
金币:1.0
发贴:189
#22001/3/22 18:31:08
这是什么东东?



莫特探员

职务:普通成员
等级:1
金币:1.0
发贴:255
#32001/3/23 13:43:54
好像没有完吧。 flyingbird 。
很不错,这个人分析的很细致。



Call Me Simon

职务:普通成员
等级:2
金币:2.0
发贴:646
#42001/3/24 8:56:40
莫特:

是的, 这是一系列文章的第一篇。我上次转贴的时候, 漏了最后一句。

编辑历史:[这消息被flyingbird编辑过(编辑时间2001-03-24 09:07:01)]


Call Me Simon

职务:普通成员
等级:2
金币:2.0
发贴:646
#52001/3/24 9:03:01
感谢野火寒烟的翻译, 翻译的质量很高, 我因为工作的原因没有足够的时间来翻译, 不过如果论坛的朋友英文好的, 鼓励大家来翻译, 如果需要的话, 我可以帮助校对。建议Lakesea 和机器猫,把这篇译文放在5d的首页。

Lingo与ActionScript的比较
(第一部分)
作者:Gary Rosenzweig
翻译: 野火寒烟

最近,Director与Flash之间的竞争和由之而起的困扰很多。他们都是Macromedia的产品,都是用于影片的制作,很多时候这些放在网上播放的影片都被称为shockwave。一些Director开发者开始转向使用Flash了。另外一些人,就像我这样,这两个工具都在使用。我想如果写一系列专栏来比较Director与Flash两个软件的脚本语言,一定是个好主意。这对已经掌握其中一种软件的程序员来说非常有用,对另一部分的人来说也希望同样能对此感兴趣。

* 它们从何而来?

Lingo,对于像Java这样的语言来说已经是一种相当老的语言了。它是由Macromedia(Macromind)公司的John Thompson创建的。对我而言,Lingo看上去就像是根植于Hypercard这种脚本语言,(Hypercard就是那种早期苹果时代Director最大的竞争者)。我猜想它起源于BASIC语言,因为BASIC语言也是一种被设计成为非符号与数字的,而更贴近英语的一种语言。


最重要的,是Lingo这个语言,跟它最初的版本相比,已经经历几次跳跃式的发展,变化巨大。对于一些语言,像BASIC、Pascal与C,开发时,已非常接近全功能工具,但其发展只是在处理新硬件方面有一点点改进。与之相反的是,现在的Lingo和Director4的Lingo相比,改进之多,看上去已经像另一个完全不同的语言了。

ActionScript是个非常新的语言。你或许会说它只是去年才和Flash5一起出现的。的确,Flash4也内嵌脚本语言,但只是那种通过下拉菜单与输入文本框方式操作的对话语言,看上去跟Flash5的actionScript完全不一样。
按说,ActionScript是基于javascript的。然而,我却发现它更接近于Java或者C++语言。我想这种类似javascript的说法,对于Macromedia来说,不过是一种市场宣传的手法而已。


* 它们的发展方向如何?

在Director里面,脚本通常就是单独的cast members。这些cast members可以被放在一个分镜窗口(Score window)的帧(frame)上,或是舞台中的一个精灵(sprite)上。当然你也可以选择把脚本作为一些特殊的cast member的一部分。

因为大多数脚本的使用都是基于他们自己的角色演员(cast member),所以你可以把同样的脚本演员放在多个帧或是精灵上。通常这被称为"行为"(behavior)。这个强大的功能的最大优势在于,你可以将一个行为脚本用在上百个精灵上面。如果修改这个脚本,所有的这些脚本都会相应更新。同时,每个精灵相对于该行为属性的值却会被保留。这是真正的面向对象的程序。


在Flash里面,脚本没有单独对应的库元素。相反,脚本必须明确地放在时间轴的一个帧上,或是一个MovieClip里。这就是说,一个脚本只能存在于一个地方。所以,如果你把一段脚本放在舞台上的一个MovieClip里,这段脚本就只存在于该MovieClip。如果你想把这段脚本用于其他片断上,你可以拷贝然后粘贴,但它们将是两个单独存在的脚本。这就使ActionScript有别于我所喜欢的面向对象语言,当然你可以用下面的解决方案, 用一个只有一行脚本的movieClip在主时间线上来调用某个function.

* 全局变量和局部变量

我们常常听到这样的说法,如果你想知道在Flash里怎么样做出你想要的东西,你可以试着想想如果在Director里该如何做, 然后就做与之相反的方式。对变量来说,这个思路完全正确!Lingo假定所有变量均为局部变量,除非像这样声明改变量为全局变量:
global gMyGloval
而ActionScript则假定所有变量均为全局变量,除非你通过赋值来声明它为局部变量:
var myLocalVar = 0;
通常,在Flash里,我就让所有变量都保持为全局变量。这有助于使我程序编写变得更快,但如果你调用的功能中有一个变量名与你使用的其他元素名相同时,就会有麻烦。

Flash同样还有层的概念。根层就是主时间线 (注:类似于根目录, 它的结构是向下的),但是任何MovieClip都可以在主时间线下创建一个完整独立与根层的新的层级。一个全局变量只能是它所在层级上的全局变量。所以,一个movieClip里的全局变量不会影响主时间线上的全局变量。

* 注释

在Lingo语言里,你会用一个双减号符号(--)来注释掉一行代码。在ActionScript里,你是用双斜线(//)来注释代码。

* 消息窗口与输出窗口

在Director里,我们有消息窗口(message window)。任何put指令(无需 into )的使用, 都会导致一些信息被放在消息窗口中显示。这个对测试程序以及快速编辑面向结果的程序非常有用。

Flash在输出窗口(output window) 很多类似的东西。不同的是, 这次使用trace指令,而不是put指令。

另外不同于消息窗口的是,你不可以在输出窗口中输入任何信息。就像它的名字所说的一样,它只是用于输出。还有,trace指令只处理一个单独的条目,不像put指令那样只要用逗号分开就可以处理多个参数。在输入窗口中,如果想在一行里取得一个以上的条目,你就必须用字符串来串起来, 例如trace(a + " " + b)

比较
Lingo用等号(=)来赋值或是比较两个变量。举例来说,你可以用下面一行语句来给一个变量a赋值5:
A = 5
你也可以用下面这句来测试变量a是否等于5:
if a = 5 then

ActionScript用两个等于号(==)来对两个变量进行比较。这一点跟C,C++或者Java非常相似:
If (a == 5)
注意,ActionScript里一个比较关系要使用圆括号括起来。如果你不这样做,会导致相当的麻烦。
在Lingo里,你可以用大于小于号(<>)来判断一些值不相等。而ActionScript是用(!=)感叹号与等号来进行判断的。惊叹号在ActionScript里这样的用法在Lingo里是不可以的。另外还有一些符号,像< and >,在两种语言里的使用规则却是一样的。



累加
Lingo的确没有办法直接使用累加功能。举例,如果你想在Lingo里给一个变量a加1,你不得不这样做:
a = a + 1

对任何曾经使用过C、C++或者Java的人来说,的确很让人受不了。而在ActionScript里,你可这样声明:
a++;
这个++符号表示将此变量加1。你同样可以这样做来累加超过1的数值:
a += 5 ;
这条语句可以给变量a加5。你也可以使用++、 --, 或者是+=, -=, 乘法甚至除法。
另外,像++这样的语法结构更容易输入,你可以使用它在其他一些代码里。举例来说,下面这句话,是想将变量a的值在输出窗口中打印出来,并且在跟踪命令结束后给它加1:
trace (a++);
如果你想在跟踪命令执行前对a进行累加,你所要做的只是简单地在a之前加上++符号而不是在它后面,像这样:
trace (++a);

编辑历史:[这消息被flyingbird编辑过(编辑时间2001-03-24 09:08:29)]


bear_5d

职务:普通成员
等级:2
金币:1.0
发贴:328
#62001/3/25 21:57:54
感谢二位为我们爱好者作出努力!真是学习dr+english的好帖!


我想…… 我做…… 我是…… 我努力,我飞翔……