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

Call Me Simon

职务:普通成员
等级:2
金币:2.0
发贴:646
注册:2000/9/19 13:56:51
#12001/3/24 11:02:58
Comparing Lingo and ActionScript, Part II

by Gary Rosenzweig

In last week's article, we started comparing Director's Lingo and ActionScript in Flash by looking at script placement, variable scoping, and operators. In this week's installment, we'll finish off our look at the two scripting languages.

Code Framing
In Lingo, the term end is used to frame a section of code. It is used at the end of a loop or at the end of a handler. In ActionScript, the curly brackets or braces {} are used just like in C++ or Java.

Handlers and Functions
In Lingo, everything is in a handler. All handlers start with on which is then followed by the name of the handler. This could be an event, like on mouseUp or some custom handler like on computeScore.

In ActionScript, functions take the place of handlers. They use the word function in place of on and are framed with braces instead of on...end.

In addition to functions, there are clip event handlers that are only valid when used as part of a movie clip instances object script. These start with onClipEvent followed by the name of a specific event, like enterFrame. Here is an empty one:

onClipEvent (enterFrame) {

// nothing

}

This is the same as the Lingo enterFrame handler, except that you can't place it on a frame. Instead, it is attached to a movie clip instance in a manner similar to how a behavior would be attached to a sprite in Director.

In addition, Flash buttons can have button event handlers in their scripts. To make it a little more confusing, these do use the on syntax. So a button script may look like this:

on (press) {

// nothing

}

Loops
Director uses script-like syntax for loops. The key word is repeat. A typical loop will look like this:

repeat with i = 1 to 10

-- do something

end repeat

In Flash, you use the more common for keyword, along with syntax like you would use in C.

for (var i = 1; i <= 10; i++) {

// do something

}

The three parts of the for statement parameters define the loop. The first part declares a variable i and sets its initial value. The second part is the condition under which the loop will continue to loop. The third part is the change that will occur after each and every loop.

Lingo also has repeat while loops. These are do while loops in ActionScript.

Strings
ActionScript string handling is pretty similar to Lingo. Instead of the & symbol for concatenating two stings, just use a + symbol. Unfortunately, there is no equivalent for the && command.

One of the cool, somewhat hidden features of ActionScript is its ability to use the += assignment to add something to the end of a string. This is the same as put ... after in Lingo:

myString += ".";

Lists and Arrays
Lingo uses lists, which Director programmers will explain to other programmers is our term for arrays. There are two types of lists: linear lists and property lists.

ActionScript uses arrays instead of linear lists. To add an element to the end of a linear list, you use the push command. To remove one, you can use pop (last item), shift (first item), or splice (arbitrary item). The equivalents in Lingo are add and deleteAt. ActionScript uses the length property to get the number of elements in an array while Lingo uses count.

There are no property arrays in ActionScript. However, you can create data objects that are similar, but less versatile. For instance, consider this ActionScript line:

myObject = {name: "Apple", color: "Red", number: 7};

Now you can call on parts of this object like this:

trace (myObject.name);

Speed
For speed, there is no comparison. Lingo beats ActionScript by a mile. As an example, I wrote a simple piece of code that slowly finds all of the prime numbers between 1 and 1000. The Lingo and ActionScript match each other line for line. You can find the source code as part of the download at the end of this column.

ActionScript takes 15 seconds to find all of the primes. You'll notice that the code has a line commented out that will put each prime found into the Output window. I decided that the trace command shouldn't be part of the test.

Similarly, I took the put command out of the Lingo code. The result is about 0.33 seconds. That's 45 times faster.

This one test doesn't mean everything, but take my word for it as an experienced programmer of both languages: Lingo is much faster.

Which One to Use
Lingo and ActionScript are similar and yet different. So, which one should you use? The answer is . . . there is no answer. Perhaps one language is easier than another for you to learn. Perhaps using the smaller Flash plugin fits better with your plans. Or, perhaps Director's expandability with Xtras is necessary for your project.

This is becoming the question that I am asked most: Should I use Director or Flash? The only answer I have is that in some cases the choice will be obvious. In other cases it will not. In those situations, go with whichever tool you know best.


编辑历史:[这消息被flyingbird编辑过(编辑时间2001-03-26 04:21:07)]


真傻

职务:普通成员
等级:3
金币:10.0
发贴:1018
注册:2001/2/20 21:54:56
#22001/3/25 12:41:15
感谢flyingbird对我的鼓励与支持,并且感谢他有这么大的耐心帮我修改我的文章。

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

上篇文章,我们开始分析了Director里的Lingo语言与Flash里的ActionScript语言,并且对这两种语言里脚本放置、变量范围和操作符进行了比较。这篇文章,我们将结束对这两种脚本语言的对比。


* 代码结构
Lingo语言里,end这一符号是用来结束某一段代码,通常在一段循环或是句柄的结束时使用。(注:句柄的英文原意是针对某一事件的处理, Lingo是一种由事件来驱动的语言)而在ActionScript语言里,是用大括号{},这种使用跟C++或者Java一样。

* Handlers 和 Functions
Lingo里,所有的指令都是在句柄里(handler)。而且所有的句柄都是以“on”开头, 然后是该句柄的名称,它可能是一个lingo内建的事件,如mouseUp,也可以是自定义的句柄名,例如ComputeScore。

在ActionScript,Function代替了lingo里的handler, function这个关键词代替了lingo里的"on","{}"符号代替了On...end 的结构。

除了function 以外,还有只对movieClip object的脚本有效的"Clip事件的handler",这种情形下的写法是由onClipEvent开始,然后跟着一个特殊的事件名, 例如enterFrame。下面是一个例子:

onClipEvent (enterFrame) {
// nothing
}

这个很类似于Lingo里的enterFrame handler,只是你不能把它附在一个帧上,相反,他必须负载一个movieClip的instance上, 就好像Director里的行为(behavior)需要附在精灵(sprite)上一样.

另外,Flash按钮的脚本里还可以有按钮事件的handler,特别让人感到容易混淆的是, 这种情形下的书写方式也是用"on"格式。例如一个按钮的脚本可能是这样:

on (press) {
// nothing
}


* 循环
Director里使用脚本式的语句来表示循环。关键字是repeat。下面是一个典型的循环语句:
repeat with i = 1 to 10
-- do something
end repeat

在Flash里,你却使用更为常见的关键字"for"以及类似C语言的句型来完成循环。

for (var i = 1; i <= 10; i++) {
// do something
}

这个for语句是用三个参数来定义循环的。第一部分是声明一个变量i,并设定它的初始值。第二部分是执行循环的条件。第三部分是每次循环后所发生的变化。

Lingo也用到repeat while循环,相应的ActionScript里是do while循环。

* 字符串
ActionScript字符串处理跟Lingo里的非常相似,不过不是用&符号而是用加号+来连接两个字符串,不幸的是,ActionScript里没有类似&&的指令。

ActionScript还有个很棒但却鲜为人知的特性,那就是它可以用+=定义符号在一个字符串的末尾加上些什么,这个功能跟Lingo的put...after相同:

myString += ".";


* 列表与数组
Lingo使用列表,Director程序员会向其他程序员解释说,这是在处理数组时我们所使用的方式。列表方式有两种:线性列表与属性列表(Linear List and Property List)。

ActionScript使用数组而不是线性列表。如果要在一个线性列表后面加上一个元素,你可以用push命令。而想删除一个元素,你可以用pop命令(删除数组里的最后一个元素),shift命令(删除数组里的第一个元素),或者splice命令(删除数组里的任意一个元素)。在Lingo里相应的指令是add 和deleteAt。ActionScript使用length属性来取得一个数组里元素的总数,而在Lingo里是使用count。

在ActionScript里没有“属性数组”。可是,你可以创建类似但不够灵活的的数据对象。举例来说,考虑一下这个ActionScript语句:

myObject = {name: "Apple", color: "Red", number: 7};

现在,你可以调用这个对象的某一个部分,像这样:

trace (myObject.name);


* 速度
对速度而言,ActionScript和lingo是无法相比的,ligno要快得多得多。我写了一段简单的代码来找出1到1000之间所有的素数。Lingo与ActionScript的代码每一行都是一样的。源文件在本文的最后有下载连接。

ActionScript花了15秒来找出这些素数。你可能会注意到,代码有一行语句是被注释了的,它将每个找到的素数显示在输出窗口中的。我想trace命令不应该是这个测试的一部分。

同样,我将Lingo代码里的put指令也注释起来。结果是用了0.33秒,比ActionScript快了足足45倍。

这个测试并不能说明全部,但请相信我 - 一个对于两种语言都富有经验的程序师的话:Lingo的速度要快的多。

* 到底用哪种语言

Lingo与ActionScript有相似也有不同之处。所以,到底你要使用哪种语言呢?答案是:没有答案。可能某种语言对你而言比较容易学;也许小的Flash插件对你的项目更合适些;又或者,Director通过Xtras而来的扩展功能对你的项目来说是必须的。

这也成为我常常问自己的问题:我是用Director,还是该用Flash?我仅有的答案就是:某些时候选择是显而易见的,而另一些时候选择就不是件容易事了,这个时候,就要选择你最熟悉的工具。