`
lixinye0123
  • 浏览: 323350 次
  • 性别: Icon_minigender_1
  • 来自: 温州
社区版块
存档分类
最新评论

1.3. 在哪里写ActionScript 代码呢

阅读更多

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.

 
分享到:
评论

相关推荐

    actionscript cook book 中文版

    1.3. 在哪里写ActionScript 代码呢 5 1.4. 如何跟踪信息 8 1.5. 处理事件 9 1.6. 响应鼠标和键盘事件 10 1.7. 算术运算 12 1.8. 逻辑运算 13 1.9. 执行条件语句 15 1.10. 执行复杂的条件语句 17 1.11. 某段时间重复...

    SWiX Free 1.3.0.1927绿色免费版

    SWiX Free是一款免费SWF编辑软件,准确的说是一款调试、更新SWF...简单的说,SWiX Free可以XML格式打开任意SWF文件,用一种"预览机制"来修改SWF元素,更新后可保存为SWF或SWIX文件,比如可用来调试 ActionScript代码。

    EmEditor的120多个语法高亮文件(esy)

    apache1.3.esy apache2.0.esy arm7.esy asp.esy atmel.esy autoit3.esy autolisp.esy awk.esy bash.esy batch.esy bibtex.esy cadkey.esy cfml.esy ch.esy Clarion.esy cnc.esy cobol.esy ctags....

    ActionScript开发技术大全

    1.1.3ActionScript3.0代码组织 5 1.2ActionScript3.0API概览 5 1.3小结 8 第2章搭建ActionScript3.0开发环境 9 2.1搭建基于FlashCS3IDE的开发环境 9 2.1.1安装FlashCS3ID 9 2.1.2安装FlashCS3IDEupdate9.0.2 11 ...

    精通Flex 3.0——基于ActionScript 3.0实现_源代码

    《精通Flex 3.0——基于ActionScript 3.0实现》一书源代码。 Flex 3.0 ActionScript 3.0源代码 Flex 3.0源代码。 --------------------------- 第1篇 Flex技术概述 第1章 Flex概述 3 1.1 Flex简介 3 1.2 Flex...

    [Flash.ActionScript.3.0动画教程

    [Flash.ActionScript.3.0动画教程],这是一本由Keith Peters编写的一本动画设计教材,此书要求读者对as2.0要有比较深的了解,主要讲解的是动画相关的原理。绝对有价值的一本书。 目录如下: 第一部分ActionScript...

    ActionScript 3.0开发技术大全(第三部分,共3部分)

    第三部分(共3部分) 第1篇ActionScript3.0语言基础 ...1.1.3ActionScript3.0代码组织 5 1.2ActionScript3.0API概览 5 1.3小结 8 第2章搭建ActionScript3.0开发环境 9 2.1搭建基于FlashCS3IDE的开发环境 9 ......

    flex3的cookbook书籍完整版dpf(包含目录)

    20.4节在JavaScript中调用ActionScript方法函数 20.5节经由BrowserManager改变HTML页面标题 20.6节BrowserManager解析URL 20.7节经由BrowserManager深度-链接到数据 20.8节经由BrowserManager深度-链接容器 20.9节...

    Foundation Actionscript 3.0 Animation

    8.3.4弹性在哪儿 8.3.5弹性链 8.3.6多目标点弹性 8.3.7目标偏移 8.3.8使用弹性贴加多个物体 8.4本章重点公式 5 8.5小结 第9章碰撞检测 9.1碰撞检测方法 9.2hitTestObjet和hitTestPoint1 529.2.1碰撞测试两个精灵 ...

    AcitonScript 3 游戏编程大学(中文版)

    1.3Flash CS3 的使用 1.4 脚本编辑区 1.5 ActionScript 游戏编程策略 1.6 脚本基本概念 1.7 测试和调试代码 1.8 发布设置 1.9 脚本游戏编程检验表 2. ActionScript 游戏基础 2.1 创建可视对象 2.1.1 影片剪辑的应用 ...

    FLASH MX Professional 2004应用开发

    1.3 使用Tools面板 1.4 添加文本 1.5 发布Flash文档 1.5.1 创建包含SWF的SWF文件和HTML页 1.5.2 添加Flash Player版本枪测 1.5.3 在现存的网页中嵌入SWF 1.6 将要添加的内容分层 1.7 为层添加内容 1.8 添加图片 1.9 ...

    Flex新手教程_入门级学习笔记

    1.3构造方法Constructor 2 1.4变量和值 Variable 2 1.5赋值 2 1.6 AS数据类型 3 1.7数据类型转换 3 1.8实例方法,实例变量和静态方法,静态变量 4 1.9接口 Interface 4 1.10继承 Inheritance 5 2.ActionScript3.0常用...

    Flex企业应用开发实战源代码

    2.1.2 查看由MXML文件所翻译的ActionScript代码 24 2.1.3 IMXMLObject接口 25 2.2 客户端保持状态 28 2.3 客户端MVC 30 .2.4 数据绑定 34 2.4.1 实现数据绑定的方法 35 2.4.2 数据绑定发生的时机 37 2.4.3 ...

    FLEX从入门到精通.pdf

     1.3 受欢迎的Flex  1.3.1 揭秘Flash和Flex  1.3.2 用户喜爱Flex的十大理由  1.4 本章小结  第2章 Flex3生态系统  2.1 Flex历史简介  2.2 Adobe Flex3生态系统,  2.2.1 运行时  2.2.2 语言  ...

    FLEX企业应用开发实战.part1

     2.1.2 查看由MXML文件所翻译的ActionScript代码  2.1.3 IMXMLObject接口  2.2 客户端保持状态  2.3 客户端MVC  2.4 数据绑定  2.4.1 实现数据绑定的方法  2.4.2 数据绑定发生的时机  2.4.3 可以用于...

    FLEX企业应用开发实战.part2

     2.1.2 查看由MXML文件所翻译的ActionScript代码  2.1.3 IMXMLObject接口  2.2 客户端保持状态  2.3 客户端MVC  2.4 数据绑定  2.4.1 实现数据绑定的方法  2.4.2 数据绑定发生的时机  2.4.3 可以用于...

    Flash+Flex+Air移动开发入门经典 pdf

    1.2 actionscript 3.0 2 1.2.1 ecmascript 2 1.2.2 关键概念 3 1.3 flex框架 11 1.3.1 flex 4.5.1 11 1.3.2 mxml 12 1.3.3 spark库组件 14 1.3.4 数据绑定 21 1.3.5 flex移动应用程序结构 22 1.3.6 移动...

    Flash商业广告设计 附书源码 007/007

    1.3多媒体音效的添加和动画的发布 多媒体音效的添加 动画的发布和浏览 1.4FlashMX2004ActionScript动作脚本 FlashMX2004ActionScript面板介绍 FlashMX2004ActionScript的格式和注释 对象和路径 动作代码详解 1.5本章...

    Flash商业广告设计 附书源码 006/007

    本书围绕Flash在商业广告方面的应用,全面讲解了商业宣传广告、网站片头动画及全Flash网站等的制作方法。 第1章Flash基础知识和动画原理 1.1FlashMX2004基础知识 初始界面与新建文档 界面布局和自定义界面 首选参数...

Global site tag (gtag.js) - Google Analytics