您的位置:首页 > 移动开发 > Objective-C

AS3.0教程(1):与Flash9先来一次亲密接触!

2006-11-24 09:49 549 查看
 <P>Flash Professional 9 Action<FONT size=+0>script</FONT> 3.0 Preview 版本今天发布了,意味着从此我们从此不仅仅只能使用Flex 2来使用AS3.0,更可以使用我们一直很熟悉的Flash IDE来进行AS3.0开发了。

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

先跳开一些特色的介绍,单刀直入,马上来个实例。边讲解边说说Flash 9的特点。
<A href="http://www.kingda.org/blog/archive/imgs/book/1-thumb.gif" target=_blank><IMG onmousewheel="function anonymous()
{
var zoom=parseInt(this.style.zoom, 10)||100;zoom+=event.wheelDelta/12;if (zoom>0) this.style.zoom=zoom+'%';return false;
}" title=按此在新窗口浏览图片 style="ZOOM: 51.83%" src="http://www.kingda.org/blog/archive/imgs/book/1-thumb.gif" border=0></A>
点击看大图,清楚点。

为了照顾新手xdjm们,看图说话一把。老鸟略过勿看,省得嫌我罗嗦。呵呵。
新建一个fla,随便画一个方块什么的,双击选中按F8转换成MovieClip。在属性面板中命名为kingda_mc。和以前一模一样。
再新建一层,命名为actions,这是个好习惯,要保持。选中第一帧,按F9打开动作面板,写入如下代码。</P>
<P><FONT color=#000000>kingda_mc.doubleClickEnabled</FONT> = true;</P>
<P>kingda_mc.addEventListener(MouseEvent.DOUBLE_CLICK, clickHandler);</P>
<P>function clickHandler(event:MouseEvent):void { trace("哈哈,你双击我了");}</P>
<P>Control+Enter,在测试窗口中,双击那个方块,就会有trace信息显示出来。

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

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

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

3.<B>AS3.0中的事件模型和AS2.0大不一样了。</B>
简而言之,就是“规范”。不再直接使用字符串来定义事件名称了。又要讲深一点了,都是使用了新的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,关闭的。

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

本篇主要涉及了一下AS3.0中的事件模型部分,这是很重要的。以后会有更深入的教程来详细介绍。本篇的目的就是让大家使用一下Flash 9和AS3,消除陌生感。写的浅了,还请包涵。
下一篇介绍非常实用的东东,类和MovieClip的绑定,和Flash 9中一大特色:Document Class。用来替代在时间轴写代码的好东东。</P>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息