当前位置:文档之家› 外文翻译

外文翻译

《动作脚本》,摘自自维基百科,

自由的百科全书

2009年5月10

《ActionScript》Designed by Gary Grossman

1998年9月

英文译文:

动作脚本

ActionScript是 Macromedia(现已被Adobe收购)为其Flash产品开发的,最初是一种简单的脚本语言,现在最新版本3.0,是一种完全的面向对象的编程语言,功能强大,类库丰富,语法类似JavaScript,多用于Flash互动性、娱乐性、实用性开发,网页制作和RIA应用程序开发。

ActionScript 是一种基于ECMAScript的脚本语言,可用于编写Adobe Flash动画和应用程序。由于ActionScript和JavaScript都是基于ECMAScript语法的,理论上它们互相可以很流畅地从一种语言翻译到另一种。不过JavaScript的文档对象模型(DOM)是以浏览器窗口,文档和表单为主的,ActionScript的文档对象模型(DOM)则以SWF格式动画为主,可包括动画,音频,文字和事件处理。

历史

在Mac OS X 10.2操作系统上的Macromedia Flash MX专业版里,这些代码可以创建一个与MAC OS X启动过程中看见的类似的动画。ActionScript第一次以它目前的语法出现是Flash 5版本,这也是第一个完全可对Flash编程的版本。这个版本被命名为ActionScript1.0。Flash 6通过增加大量的内置函数和对动画元素更好的编程控制更进一步增强了编程环境的功能。Flash 7(MX 2004)引进了ActionScript2.0,它增加了强类型(strong typing)和面向对象特征,如显式类声明,继承,接口和严格数据类型。ActionScript1.0和2.0使用相同的编译形式编译成Flash SWF文件(即Shockwave Flash files,或 'Small Web Format').

时间表

Flash Player 2:第一个支持脚本的版本,包括控制时间轴的gotoAndPlay, gotoAndStop, nextFrame和nextScene等动作。

Flash Player 3:增强了载入外部SWF文件的基本脚本支持(loadMovie)。

Flash Player 4:第一个完全实现脚本功能(称为动作)的播放器。这些脚本拥有简

练的语法和对循环,条件,变量和其它基本语言结构的支持。

Flash Player 5:第一个拥有真正意义上的ActionScript的版本。依据ECMAScript 并采用基于原型编程,并允许完全的过程式编程和面向对象编程。

Flash Player 6:增加了事件处理模型,并且支持switch。

Flash Player 7: Flash Player 7 提供一些新特性如支持CSS显示文本和增强显示效果。 Macromedia Flash编译器和Flash Player 7同时支持基于ECMAScript 4 Netscape Proposal的类编程语言ActionScript 2.0。不过ActionScript 2.0能交叉编译成ActionScript 1.0的字节码,因此它能运行于Flash Player 6。

Flash Player 8:增加用于运行时图象数据控制和文件上传的新类库及APIs,ActionScript 2.0功能更为完善。

Flash Player 8.5(计划于2006春发布release版本,currently in public beta):增加ActionScript 3.0和一个称为AVM2(ActionScript Virtual Machine 2)新的虚拟机,它可以与前版本AVM1共存以便支持旧内容。增强性能是该版本的主要目标。Flash Player 9 2007发表,这是adobe收购Macromedia后的第一个版本,并且也是Mac OS X上第一个Universal版本的Flash Player。[1]

语言

语法

在ActionScript 2.0 类、与函式馆物件(如影片片段)可与类联系在一起。类总写在外在文件档案内,并且这些文件必须有.as后缀。类是增设部分──对ActionScrip 语言来说,可让程式员自行开发,虽然有许多内建类譬如MovieClip类──可被用来动态地在屏幕上画出向量──已经满足需求了。类档案可用来使您编程更加容易,并且类档案如果需要的话可在许多专案间转移。

Flash ActionScript实现的特点如下,JavaScript程式员也许感兴趣:

一切设计是异步的;callback是普遍存在的,但事件对象不存在。

XML的实现从Flash 5便存在了。Flash可送和收XML,该项功能可用来透过网络服务器创造网上多玩者游戏。

ActionScript 代码常常直接写在Flash开发环境。该环境提供参考、代码提示和句法强调。原代码常常与电影一起存在.fla档案里。自外部文件档透过#include语法导入ActionScript代码也相当常见。在这种情况下,外部档案也许被Flash集成开发环境内建编译器,或Motion Twin ActionScript2编译器(MTASC)编译。参见外部链接。

评论

编程人员们说Macromedia ActionScript 2.0编译器有点慢,常常花好几分钟才编译100个类,然而开放源码编译器MTASC可以利用;它快多了。

ActionScript非常宽大的语法常常让编程人员们皱眉,因为它常常让不干净代码难以阅读。在Flash里使用许多向量可能拖慢执行许多应用程序的机器的效能,因为Flash每帧每帧重画每个向量。Flash 8引进了cacheAsBitmap 变量,它暂时把向量转换到位图,这种做法帮助降低了延滞。

Flash的ActionScript VM倾向在触发内部的暂停处理前就非常快达到它的运算极限,特别在麦金塔版的Flash播放器上更严重。例如,简单从1算到5000会威胁某些用户Flash播放器的能力。

在Flash 8里许多人不喜欢引入(import)某些类直到要用到那些类前最后一刻。不幸的是,ActionScript 3.0相当倚赖引入类,没有导入撰写脚本(scripting)实际上不太可能。

.swf档案格式挺容易反组译,使它非常难以保持源代码机密。

样本

ActionScript 2.0 样本

下面打印Hello World。值得注意的是这只能在整合环境下执行,因为trace函式只有支援整合环境。

trace("Hello world!");

下面代码利用onMouseMove事件当鼠标移动时输出现行鼠标位置。同样的这只有在整合环境下执行。

onMouseMove = function () {

trace("X: "+_root._xmouse);

trace("Y: "+_root._ymouse);

};

这个较先进的范例创造一个包括数字与字串的阵列,并利用原形函式(prototype function)与函式递归给变量名num指定一个数,给变量str指定一个字串。然后,利用MovieClip应用程序接口,文字区域被显示在屏幕上,文字区域里头写入了变量值。

var my_Array:Array = new Array("Hello", "ActionScript", 3, 7, 11, "Flash");

Array.prototype.pickNumber = function():Number {

var rand:Number = random(this.length);

return (typeof (this[rand]) == "number") ? this[rand] : this.pickNumber(); };

Array.prototype.pickString = function():String {

var rand:Number = random(this.length);

return (typeof (this[rand]) == "string") ? this[rand] : this.pickString(); };

var num:Number = my_Array.pickNumber();

var str:String = my_Array.pickString();

_root.createTextField("txt", 1, 10, 10, 530, 390);

txt.text = "Array = "+my_Array+"\nRandom Number = "+num+"\nRandom String = "+str;

ActionScript 3.0 样本

下面先进的Hello World程序目前需要在Flex 2.0公开Alpha测试版整合环境中编译。

package {

import flash.display.TextField;

import flash.display.MovieClip;

import flash.filters.DropShadowFilter;

public class HelloWorld extends MovieClip {

public function HelloWorld() {

var shad:DropShadowFilter = new DropShadowFilter(2, 45, 0x000000, 25, 3, 3, 2, 2);

var txt:TextField = new TextField();

txt.textColor = 0xFFFFFF;

txt.filters = [shad];

txt.width = 120;

txt.x = Math.random()*300;

txt.y = Math.random()*300;

txt.selectable = false;

txt.text = "Hello World! ["+Math.round(txt.x)+","+Math.round(txt.y)+"]";

addChild(txt);

}

}

}

英文原文:

ActionScript

ActionScript is a scripting language based on ECMAScript. ActionScript is used primarily for the development of websites and software using the Adobe Flash Player platform (in the form of SWF files embedded into Web pages), but is also used in some database applications (such as Alpha Five), and in basic robotics, as with the Make Controller Kit. Originally developed by Macromedia, the language is now owned by Adobe (which acquired Macromedia in 2005). ActionScript was initially designed for controlling simple 2D vector animations made in Adobe Flash (formerly Macromedia Flash). Later versions added functionality allowing for the creation of Web-based games and rich Internet applications with streaming media (such as video and audio).

History

ActionScript started as a scripting language for Macromedia's Flash authoring tool, now developed by Adobe Systems as Adobe Flash. The first three versions of the Flash authoring tool provided limited interactivity features. Early Flash developers could attach a simple command, called an "action", to a button or a frame. The set of actions was basic navigation controls, with commands such as "play", "stop", "getURL", and "gotoAndPlay". With the release of Flash 4 in 1999, this simple set of actions became a small scripting language. New capabilities introduced for Flash 4 included variables, expressions, operators, if statements, and loops. Although referred to internally as "ActionScript", the Flash 4 user manual and marketing documents continued to use the term "actions" to describe this set of commands .

Timeline by player

Flash Player 2: The first version with scripting support. Actions included gotoAndPlay, gotoAndStop, nextFrame and nextScene for timeline control.

Flash Player 3: Expanded basic scripting support with the ability to load external SWFs (loadMovie).

Flash Player 4: First player with a full scripting implementation (called Actions). The scripting was a flash based syntax and contained support for loops, conditionals, variables and other basic language constructs.

Flash Player 5: Included the first version of ActionScript. Used prototype-based programming based on ECMAScript, and allowed full procedural programming and object-oriented programming.

Flash Player 6: Added an event handling model, accessibility controls and support for switch. The first version with support for the AMF and RTMP protocols which allowed for ondemand audio/video streaming.

Flash Player 7: Additions include CSS styling for text and support for ActionScript 2.0, a programming language based on the ECMAScript 4 Netscape Proposal with class-based inheritance. However, ActionScript 2.0 can cross compile to ActionScript 1.0 byte-code, so that it can run in Flash Player 6.

Flash Player 8: Further extended ActionScript 1/ActionScript 2 by adding new class libraries with APIs for controlling bitmap data at run-time, file uploads and live filters for blur and dropshadow.

Example of ActionScript 2.0 running on Macromedia Flash 8.Flash Player 9 (initially called 8.5): Added ActionScript 3.0 with the advent of a new virtual machine, called A VM2 (ActionScript Virtual Machine 2), which coexists with the previous A VM1 needed to support legacy content. Performance increases were a major objective for this release of the player including a new JIT compiler. Support for binary sockets, E4X XML parsing, full-screen mode and Regular Expressions were added. This is the first release of the player to be titled Adobe Flash Player.

Flash Player 10 (initially called Astro): Added basic 3D manipulation, such as rotating on the X, Y, and Z axis, and a 3D drawing API. Ability to create custom filters using Adobe Pixel Bender. Several visual processing tasks are now offloaded to the GPU which gives a noticeable decrease to rendering time for each frame, resulting in higher frame rates, especially with H.264 video. There is a new sound API which allows for custom creation of audio in flash, something that has never been possible before. [2]

Timeline by ActionScript version

2000–2003: ActionScript "1.0" With the release of Flash 5 in September 2000, the "actions" from Flash 4 were enhanced once more and named "ActionScript" for the first time.[3] This was the first version of ActionScript with influences from JavaScript and the ECMA-262 (Third Edition) standard, supporting the said standard's object model and many of its core data types. Local variables may be declared with the var statement, and user-defined functions with parameter passing and return values can also be created. Notably, ActionScript could now also be typed with a text editor rather than being assembled by choosing actions from drop-down lists and dialog box controls. With the next release of its authoring tool, Flash MX, and its corresponding player, Flash Player 6, the language remained essentially unchanged; there were only minor changes, such as the addition of the switch statement and the "strict equality" (===) operator, which brought it closer to being ECMA-262-compliant. Two important features of ActionScript that distinguish it from later versions are its loose type system and its reliance on prototype-based inheritance. Loose typing refers to the ability of a variable to hold any type of data. This allows for rapid script development and is particularly well-suited for

small-scale scripting projects. Prototype-based inheritance is the ActionScript 1.0 mechanism for code reuse and object-oriented programming. Instead of a class keyword that defines common characteristics of a class, ActionScript 1.0 uses a special object that serves as a "prototype" for a class of objects. All common characteristics of a class are defined in the class's prototype object and every instance of that class contains a link to that prototype object.

2003–2006: ActionScript 2.0 The next major revision of the language, ActionScript 2.0, was introduced in September 2003 with the release of Flash MX 2004 and its corresponding player, Flash Player 7. In response to user demand for a language better equipped for larger and more complex applications, ActionScript 2.0 featured compile-time type checking and class-based syntax, such as the keywords class and extends. (While this allowed for a more structured object-oriented programming approach, the code would still be compiled to ActionScript 1.0 bytecode, allowing it to be used on the preceding Flash Player 6 as well. In other words, the class-based inheritance syntax was a layer on top of the existing prototype-based system.) With ActionScript 2.0, developers could constrain variables to a specific type by adding a type annotation so that type mismatch errors could be found at compile-time. ActionScript 2.0 also introduced class-based inheritance syntax so that developers could create classes and interfaces, much as they would in class-based languages such as Java and C++. This version conformed partially to the ECMAScript Fourth Edition draft specification.

2006–today: ActionScript 3.0 In June 2006, ActionScript 3.0 debuted with Adobe Flex 2.0 and its corresponding player, Flash Player 9. ActionScript 3.0 was a fundamental restructuring of the language, so much so that it uses an entirely different virtual machine. Flash Player 9 contains two virtual machines, A VM1 for code written in ActionScript 1.0 and 2.0, and A VM2 for content written in ActionScript 3.0. Actionscript 3.0 added limited support for hardware acceleration ( DirectX, OpenGL ).

The update to the language introduced several new features:

Compile-time and runtime type checking—type information exists at both compile-time and runtime.

Improved performance from a class-based inheritance system separate from the prototype-based inheritance system.

Support for packages, namespaces, and regular expressions.

Compiles to an entirely new type of bytecode, incompatible with ActionScript 1.0 and 2.0 bytecode.

Revised Flash Player API, organized into packages.

Unified event handling system based on the DOM event handling standard.

Integration of ECMAScript for XML (E4X) for purposes of XML processing.

Direct access to the Flash runtime display list for complete control of what gets displayed at runtime.

Completely conforming implementation of the ECMAScript fourth edition draft

specification.

Flash Lite

Flash Lite 1.0: Flash Lite is the Flash technology specifically developed for mobile phones and consumer electronics devices. Supports Flash 4 ActionScript.

Flash Lite 1.1: Flash 4 ActionScript support and additional device APIs added.

Flash Lite 2.0 and 2.1: Added support for Flash 7 ActionScript 2.0 and some additional fscommand2 API.

Flash Lite 3: Added support for Flash 8 ActionScript 2.0 and also FLV video playback. Syntax

ActionScript code is free form and thus may be created with whichever amount or style of whitespace that the author desires. The basic syntax is derived from ECMAScript. ActionScript 2.0

The following code, which works in any compliant player, creates a text field at depth 0, at position (0, 0) on the screen (measured in pixels), that is 100 pixels wide and high. Then the text parameter is set to the "Hello, world!" string, and it is automatically displayed in the player:

createTextField("greet", 0, 0, 0, 100, 100);

greet.text = "Hello, world";

When writing external ActionScript 2.0 class files the above example could be written in a file named Greeter.as as following.

class com.example.Greeter extends MovieClip

{

public function Greeter() {}

public function onLoad() :V oid

{

var txtHello:TextField = this.createTextField("txtHello", 0, 0, 0, 100, 100.);

txtHello.text = "Hello, world";

}

}

ActionScript 3.0

ActionScript 3.0 has a similar syntax to ActionScript 2.0 but a different set of APIs for creating objects. Compare the script below to the previous ActionScript 2.0 version:

var greet:TextField = new TextField();

greet.text = "Hello World";

this.addChild(greet);

Minimal ActionScript 3.0 programs may be somewhat larger and more complicated due to the increased separation of the programming language and the Flash IDE.

Presume the following file to be Greeter.as:

package com.example

{

import flash.text.TextField;

import flash.display.Sprite;

public class Greeter extends Sprite

{

public function Greeter()

{

var txtHello:TextField = new TextField();

txtHello.text = "Hello World";

addChild(txtHello);

}

}

}

(See also: Sprite)

Finally, an example of using ActionScript when developing Flex applications, again presuming the following content to be in a file named Greeter.as:

package

{

public class Greeter

{

public static function sayHello():String

{

var greet:String = "Hello, world!";

return greet;

}

}

}

相关主题
文本预览
相关文档 最新文档