1.3. 在哪里写ActionScript 代码呢

tag:ActionScript 3 CookBook 简体中文

1.3. 在哪里写ActionScript 代码呢

问题

当你有了ActionScript工程后,接着就需要知道任何输入代码。

解决方法

在类结构中或方法体中添加 ActionScript 代码

讨论

在以前的ActionScript 1.0 和 2.0中, 有多种途径添加代码:在时间线上,按钮上或电影剪辑上,在电影剪辑的时间线上通过#include命令引入外部的as文件或class文件。但是 ActionScript 3.0 是完全基于类的,所以所有的代码都必须放置在类文件中。

当你创建一个新的 ActionScript 工程后,主类文件被自动创建,并且在代码视图中代开了,刚开始的代码大概是这样的:

package {
    
import flash.display.Sprite;

    
public class ExampleApplication extends Sprite
    
{
        
public function ExampleApplication(  )
        
{

        }

    }

}

可能你很熟悉 ActionScript 2.0中的类, 但是3.0发生了很多变化,这些我们将在第二章讨论,在这里先学完基础概念先。

首先注意到代码顶层有个关键字 package ,Packages(包) 是用来组织一群相关联的类文件的。在 ActionScript 2.0, 包是用来判断类文件的路径。在 ActionScript 3.0 中必须指定包,例如,我们有个utility类包,要这样申明:

package com.as3cb.utils {

}

如果你不指明包名,那么该类就输入最顶层的默认包。

接下来,加入 import 语句,引入一个类就相当于在当前的代码文件中创建了使用该类的快捷方式,这样我们就不需要输入全路径来使用它了。例如,你可以使用下面的 import 语句:

import com.as3cb.utils.StringUtils;

这样我们就可以直接引用 StringUtils 这个类了。从flash.display 引入Sprite 类是因为默认的类文件继承了Sprite 类。

接下来就看到我们的主类 ExampleApplication,注意到在class关键字前有个关键字public ,表明该类是共有的。最后有个公共方法,方法名和主类一样,这种方法称为构造器,当一个类实例被创建时,其构造器会被自动执行,在这里,当swf文件被Flash 播放器载入时构造器就会被执行。 In this case, it is executed as soon as the .swf is loaded into the Flash player. So where do you put your code to get it to execute? Generally, you start out by putting some code in the constructor method. Here's a very simple example that just draws a bunch of random lines to the screen:

package {
    
import flash.display.Sprite;
    
public class ExampleApplication extends Sprite {
        
public function ExampleApplication(  ) {
            graphics.lineStyle(
101);
            
for(var i:int=0;i<100;i++{
                graphics.lineTo(Math.random(  ) 
* 400, Math.random(  ) * 400);
            }

        }

    }

}

保存然后运行程序,浏览器会打开一个html文件,显示一个swf里画了100条随即直线。正如你所看到的,当swf被播放器载入后构造器就会被执行。

在这里联系中,我们把代码直接写在了构造器中,但时最好的办法是在构造器中引用一个方法,在这个方法中添加代码.

对于新手来说,现在你已经学会了如何添加代码了。

Variables

Variables are convenient placeholders for data in your code, and you can name them anything you'd like, provided the name isn't already reserved by ActionScript and the name starts with a letter, underscore, or dollar sign (but not a number). The help files installed with Flex Builder 2 contain a list of reserved words. Variables are convenient for holding interim information, such as a sum of numbers, or to refer to something, such as a text field or sprite. Variables are declared with the var keyword the first time they are used in a script. You can assign a value to a variable using an equal sign (=), which is also known as the assignment operator. If a variable is declared outside a class method, it is a class variable. Class variables, or properties, can have access modifiers, public, private, protected, or internal. A private variable can only be accessed from within the class itself, whereas public variables can be accessed by objects of another class. Protected variables can be accessed from an instance of the class or an instance of any subclass, and internal variables can be accessed by any class within the same package. If no access modifier is specified, it defaults to internal.


Functions

Functions are blocks of code that do something. You can call or invoke a function (that is, execute it) by using its name. When a function is part of a class, it is referred to as a method of the class. Methods can use all the same modifiers as properties.


Scope

A variable's scope describes when and where the variable can be manipulated by the code in a movie. Scope defines a variable's life span and its accessibility to other blocks of code in a script. Scope determines how long a variable exists and from where in the code you can set or retrieve the variable's value. A function's scope determines where and when the function is accessible to other blocks of code. Recipe 1.13 deals with issues of scope.


Event handler

A handler is a function or method that is executed in response to some event such as a mouseclick, a keystroke, or the movement of the playhead in the timeline.


Objects and classes

An object is something you can manipulate programmatically in ActionScript, such as a sprite. There are other types of objects, such as those used to manipulate colors, dates, and text fields. Objects are instances of classes, which means that a class is a template for creating objects and an object is a particular instance of that class. If you get confused, think of it in biological terms: you can consider yourself an object (instance) that belongs to the general class known as humans.


Methods

A method is a function associated with an object that operates on the object. For example, a text field object's replaceSelectedText( ) method can be used to replace the selected text in the field.


Properties

A property is an attribute of an object, which can be read and/or set. For example, a sprite's horizontal location is specified by its x property, which can be both tested and set. On the other hand, a text field's length property, which indicates the number of characters in the field, can be tested but cannot be set directly (it can be affected indirectly, however, by adding or removing text from the field).


Statements

ActionScript commands are entered as a series of one or more statements. A statement might tell the playhead to jump to a particular frame, or it might change the size of a sprite. Most ActionScript statements are terminated with a semicolon (;). This book uses the terms statement and action interchangeably.


Comments

Comments are notes within code that are intended for other humans and ignored by Flash. In ActionScript, single-line comments begin with // and terminate automatically at the end of the current line. Multiline comments begin with /* and are terminated with */.


Interpreter

The ActionScript interpreter is that portion of the Flash Player that examines your code and attempts to understand and execute it. Following ActionScript's strict rules of grammar ensures that the interpreter can easily understand your code. If the interpreter encounters an error, it often fails silently, simply refusing to execute the code rather than generating a specific error message.

Don't worry if you don't understand all the specifics. You can use each recipe's solution without understanding the technical details, and this primer should help you understand the terminology.

加支付宝好友偷能量挖...


评论(0)网络
阅读(98)喜欢(0)flash/flex/fcs/AIR