现在的位置: 首页 > 综合 > 正文

ActionScript 3.0系列教程(1):与Flash9先来一次亲密接触!

2012年11月06日 ⁄ 综合 ⁄ 共 3417字 ⁄ 字号 评论关闭

Flash Professional 9 ActionScript 3.0 Preview 版本今天发布了,意味着从此我们从此不仅仅只能使用Flex 2来使用AS3.0,更可以使用我们一直很熟悉的Flash IDE来进行AS3.0开发了。

与Flex 2不同,Flash 9 alpha(即上面的Flash Professional 9 ActionScript 3.0 Preview )允许我们创建基于时间轴的ActionScript 3.0的Fla文档,而不是基于State的MXML文档。 在Flash 9 alpha 里,我们和以前一样可以在舞台上直接手绘矢量图,创建元件,添加动画,等等。

我黑羽是急性子,先跳开一些特色的介绍,单刀直入,马上来个实例。边讲解边说说Flash 9的特点。


点击放大

为了照顾新手xdjm们,看图说话一把。老鸟略过勿看,省得嫌我罗嗦。呵呵。
新建一个fla,随便画一个方块什么的,双击选中按F8转换成MovieClip。在属性面板中命名为kingda_mc。和以前一模一样。
再新建一层,命名为actions,这是个好习惯,要保持。选中第一帧,按F9打开动作面板,写入如下代码。
Control+Enter,在测试窗口中,双击那个方块,就会有trace信息显示出来。

kingda_mc.doubleClickEnabled = true;
kingda_mc.addEventListener(MouseEvent.DOUBLE_CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
   trace("哈哈,你双击我了");
}
//直接支持双击了

稍作解释,这儿有几个和AS2.0不同的地方了。

1. AS2.0中,MovieClip是不可以加侦听器地,但AS3.0中,却可以了。讲点深入的东东给老鸟听,所有AS3.0中能被我们看见的对象,其祖宗都是DisplayObject类。标准说法是都间接或直接的继承于DisplayObject类。而这个DisplayObject又是EventDispatcher的儿子。所以,我们就有了这个推论:
AS3.0中所有能被我们看到的东西,都能发送事件和加侦听器。 完全适用于Event Model.
爽吧, 我是爽歪了。AS2.0中为了解决这个麻烦我还自己编了一个代理发送事件类EventSender。省了不少事儿,而现在连这个也不用了,霍哈哈。

2.AS3.0中要让MovieClip在接受click事件,rollover事件能够像Button一样,鼠标放上去显示手型,那么一定要加上一句:
kingda_mc.buttonMode = true;
小事一桩,一笔带过。

3.AS3.0中的事件模型和AS2.0大不一样了。
简而言之,就是“规范”。不再直接使用字符串来定义事件名称了。又要讲深一点了,都是使用了新的const型变量来定义事件字符串名称,一旦定义,不能再更改。
如:

public static const MOVE:String = "move";

极大的避免了我们因为手误,打错字符串,而花上一个下午找bug。使用了这种模式,我们一旦打错,编译器立刻会发现并告诉我们。多好。
给出一些鼠标事件列表,大家可以替换上面源码中的事件类型,自己试着玩儿。
如,你可以换成MouseEvent.MOUSE_OVER就变成了以前的onRollOver效果。

 

    CLICK : String = "click"

[static] Dispatched when a user presses 
and releases the main button of the user's pointing device over the same InteractiveObject. MouseEvent 

    DOUBLE_CLICK : String 
= "doubleClick"

[static] Dispatched when a user presses 
and releases the main button of a pointing device twice in rapid succession over the same InteractiveObject when that object's doubleClickEnabled flag is set to true. MouseEvent 

    MOUSE_DOWN : String 
= "mouseDown"

[static] Dispatched when a user presses the pointing device button over an InteractiveObject instance 
in the Flash Player window. MouseEvent 

   MOUSE_LEAVE : String 
= "mouseLeave"

[static] Dispatched by the Stage object when the mouse pointer moves out of the Flash Player window area. Event 

    MOUSE_MOVE : String 
= "mouseMove"

[static] Dispatched when a user moves the pointing device 
while it is over an InteractiveObject. MouseEvent 

    MOUSE_OUT : String 
= "mouseOut"

[static] Dispatched when the user moves a pointing device away 
from an InteractiveObject instance. MouseEvent 

    MOUSE_OVER : String 
= "mouseOver"

[static] Dispatched when the user moves a pointing device over an InteractiveObject instance 
in the Flash Player window. MouseEvent 

    MOUSE_UP : String 
= "mouseUp"

[static] Dispatched when a user releases the pointing device button over an InteractiveObject instance 
in the Flash Player window. MouseEvent 

    MOUSE_WHEEL : String 
= "mouseWheel"

//支持鼠标滚轮喽,霍霍。

指出一点,在我给出的例子中,使用了双击这个事件。这个有点特殊,在使用双击事件之前,要加上一句:

kingda_mc.doubleClickEnabled = true;

因为MovieClip对于双击事件默认是false,关闭的。

4.侦听器的不同。
在AS2.0中我们通常要新建一个对象做侦听器。也可以像我的例子中用function做侦听器。但是,很可惜,由于AS2.0的设计缺陷,使得function中的this指向常常给我们带来困扰。于是有了Delegate类来解决。
而如今,AS3.0中采用了优秀的Traits Object架构(唔,这个,就暂不解释了),使得它能记住this的指向。所以,兄弟们,放心大胆使用Function作为侦听器使用吧。

今天就写这么多了,主要是Flash 9出来,我老人家激动了一下,一下子写了这么多东东。希望对大家有所帮助,希望大家狂顶支持一把,不然没动力,本系列教程会变成太监贴!霍哈哈! ^_^ 快回帖支持!

本篇主要涉及了一下AS3.0中的事件模型部分,这是很重要的。以后会有更深入的教程来详细介绍。本篇的目的就是让大家使用一下Flash 9和AS3,消除陌生感。写的浅了,还请包涵。

下一篇介绍非常实用的东东,类和MovieClip的绑定,和Flash 9中一大特色:Document Class。用来替代在时间轴写代码的好东东。

【上篇】
【下篇】

抱歉!评论已关闭.