Flash 5 Interactivity and Scripting - Softcover

Chapman, Nigel

 
9780471497813: Flash 5 Interactivity and Scripting

Inhaltsangabe

What Web developers need to know about using Flash's interactivity and scripting features Flash, one of the most popular Web technologies, is often used to build reliable dynamic, graphically rich front-ends to Web-based services and e-commerce applications. This book provides Web application developers with a complete introduction to the interactivity and the client/server interaction features provided by Flash 4. Nigel Chapman, author of "Digital Multimedia and Perl: The Programmer's Companion", explains all topics at an intermediate/advanced level, incorporating ample e-commerce applications and substantial working examples. Coverage includes Flash 4 animation concepts, an introduction to interactivity, scripting, interface elements, client/server interaction, Flash 4 generator, and a discussion of Flash and other technologies.

Die Inhaltsangabe kann sich auf eine andere Ausgabe dieses Titels beziehen.

Auszug. © Genehmigter Nachdruck. Alle Rechte vorbehalten.

CHAPTER 3: Introducing ActionScript

JavaScript, ECMAScript and ActionScript

First of all there was LiveScript, a scripting language devised by Netscape for simple Web programming tasks, especially client-side scripting jobs such as verifying input to forms and dynamically modifying aspects of pages' appearance. LiveScript changed its name to JavaScript shortly after its release, even though its actual relationship to the Java programming language is slight and the resemblance between the names has been a source of some confusion ever since. Microsoft, in its role as the other major browser manufacturer, produced its own version of JavaScript, known as JScript, which was not quite identical to Netscape's. To avoid yet another source of browser incompatibility, the European Computer Manufacturers' Association (EGMA) was called upon to produce a standard based on JavaScript and Jscript. This standard gave the name EGMAScript to the language it defined.

It was always the case that JavaScript comprised a core language, providing general purpose programming facilities and some rudimentary object-oriented features, and a set of built-in object that allowed Javascript programs to interact with some other system. In the most familiar case, the other system is a Web browser, and the built-in objects correspond to elements of Web pages and browser windows. By providing a different set of built-in objects, the same language can be used to interact with other systems. Server-side Javascript, for instance, lets scripts interact with Netscape Web servers and with files and databases.

ECMAScript is a formally defined version of the core language only. The standard explicitly concedes that ECMAScript is not 'computationally self-sufficient': it has to be combined with what it calls host objects that allow scripts to 'manipulate, customize and automate the facilities of an existing system'. Flash is an 'existing system' in the sense implied here, and ActionScript is almost ECMAScript combined with host objects that allow it to manipulate Flash movies. It is only almost that, because the need to maintain compatibility with Flash's previous scripting system has made some compromises with the ECMAScript standard necessary. However, ActionScript is close enough to ECMAScript and ECMAScript is close enough to Javascript for the syntax and core semantics of ActionScript to be immediately comprehensible to anyone who already knows Javascript.

In the previous chapter, we considered adding a single action selected from the list in the Actions panel to a frame or button instance. Generally, this inserted a single ActionScript statement into the script. Many simple Flash movies are constructed using no more than this, but to produce more elaborate effects, these primitive actions (and others that we have not considered yet) can be combined into more elaborate scripts that carry out an organized set of operations. This enables Flash movies to perform computation and to react flexibly to users' input. When we are dealing with scripts with more than one action, it makes sense to think of them as little programs written in ActionScript. We therefore need to look at ActionScript as a programming language.

Expressions and Variables

The best place to start a description of any programming language is by considering the different types of value that are available.

Values and Expressions

ActionScript recognizes three different types: number, string and Boolean. Numbers are used for arithmetic, and strings for storing and altering text, while Booleans are truth values, which can be used to control the flow of execution of a script.

Generally speaking, the way ActionScript handles numbers and arithmetic follows the way it is normally done on paper, with no arbitrary additional rules for the benefit of the computer, although some notational compromises are made with the keyboard. No distinction is drawn between integers and floating point numbers. (Internally, all operations are performed on IEEE 754 double-precision floating point quantities.) Numerical constants (or literals) are written using ordinary decimal notation, as, for example, 158 or 14.673, or scientific notation, using e to indicate a decimal exponent, as, for example, 12e35 (for 12.1035) or 1e-4 (for 0.0001). The usual operations of addition, subtraction, multiplication and division are available, using the conventional operators (conventional in programming languages, that is) +, -, * and /. Unary - can be used to negate a value, and unary + can be used to do nothing. The remainder (or modulo) operator is written as %, and differs from that provided in many languages by not being restricted to integers. That is, as well as being able to write 8%3, which gives 2, the remainder on dividing 8 by 3, you can also write 4.5%2.1, which gives 0.3, the remainder on dividing 4.5 by 2.1. Also unlike most languages, ActionScript defines what happens if the operands of % are negative: the result is negative if and only if the left operand is negative; the magnitude is the same whatever the signs.

ActionScript also deals with the tricky questions that arise from division by zero and other dubious operations. The number data type includes special values Infinity and NaN. Infinity is the result of operations whose size exceeds the range of numbers that can be represented; for example, 1/0 yields Infinity. NaN stands for 'Not a Number' and is the result of undefined operations. In particular, 0/0 produces NaN.

Because all arithmetic is done on floating point numbers, you cannot rely on the results of arithmetic operations being precise: floating point numbers are only approximations, and the way in which floating point arithmetic is carried out can introduce errors, which can accumulate if a series of operations is performed. Because the machine uses binary numbers and we use decimal, such errors often crop up in calculations that look as if they ought to be trivially precise. For example, evaluating 4.1 - 4 produces 0.0999999999999996. Numerical experts will tell you that you shouldn't really even compare floating point numbers for equality, but instead you should see whether their difference is less than some small value that gives you the precision you require.....

„Über diesen Titel“ kann sich auf eine andere Ausgabe dieses Titels beziehen.