国外看的一个介绍sprite,shape,movieclip,bitmap的好文章

One of the more drastic changes (if not the most) in ActionScript 3probably has to be working with display objects, or objects visible on thescreen. Along with the well established and popular MovieClip (flash.display.MovieClip),ActionScript 3 supports additional display object instances including but not limited to:

Sprites are a new kind of light-weight MovieClip that doesn’t technicallyhave any frames, at least not in the conventional sense.  In a more practical,Flash-like sense, you can see a Sprite as having one frame in which it keeps its contentor children, but there is no navigation to any frames beyond that first. SinceMovieClip instances you create dynamically through ActionScript cannot have frames anyway, chances are you'dwant to use a Sprite in place of a MovieClip for all dynamically createdMovieClip instances.

Shapes are like Sprites but are impeded by the fact that they offerno form of mouse or keyboard interactivity.  In other words, these displayobjects cannot receive events like mouseDown or keyUp.  Additionally, Shapeinstances cannot contain any other kind of display objects nor have children oftheir own.  You can only draw vector shapes within them using the vectordrawing API (beginFill, lineTo, endFill, etc). This comes as an advatage toShapes since they use less memory and are more efficient than MovieClips and Sprites.

shape也不不能接受到鼠标和键盘的监听器的,不能添加子对象,

Bitmap instances are much like Shapes except instead of being able tocontain vector drawings, they contain bitmap images defined by a BitmapDatainstance.  They, too, cannot contain any other display objects or children. Like Shapes, Bitmaps cannot interact with the mouse and keyboard.

记得以前我有在bitmap添加监听器,但是纠结了很久都没有弄出结果,看来如上面所说,bitmap是不能接收到鼠标和 键盘事件的。

Loader is a Sprite-like display object which, like Sprite, has no timeline, but is designed specifically to facilitate the loading of external display content such as external SWFs and bitmap images. When loaded, those assets displayed in the Loader instance and are accessible through the Loader's content property.



Timeline based symbols

Most elements on the timeline which are not text or symbols from the library are created asShapes instances.  These Shapes include not only vector drawings in Flash, butbitmap images as well; instead of using a Bitmap instance for the timeline,Flash uses a Shape instance drawn with a bitmap fill.

Shape tweens on the timeline, instead of a Shape instances, are represented by anew class called MorphShape (flash.display.MorphShape).  MorphShape instances cannot be createddynamically and offer limited interactivity through ActionScript.

As with previous versions of Flash, the only available symbol types for your library aremovie clip, button, and graphic.  When on the timeline, movie clip symbols arerepresented as MovieClip instances; buttons, instances of a top level Buttonclass in ActionScript 2, are now instances of a new class called SimpleButton (flash.display.SimpleButton);and graphic symbols become, because of their lack of interactivity, Shapeinstances.  As a Shape, however, graphic symbols are not allowed to containother children like MovieClips.  If you have an instance of a MovieClip in agraphic symbol, that MovieClip will become a child of the graphic's parent duringplayback, escaping from what would be the containing Shape instance..

For text, there are now two classes, TextField (flash.text.TextField) and StaticText (flash.text.StaticText).  TheTextField class is for dynamic and input text.  As with previous versions ofActionScript, these kinds of text fields can be created dynamically throughActionScript.  The StaticText class used to represent static text on thetimeline, however, cannot be created dynamically.  These instances are onlyavailable when a static text field has been placed on a timeline in Flash.



The most common way to gain access of text and symbol display objects on thescreen through ActionScript is to assign them an instance name.  This name is added through theinstance name field in the property inspector of the selected instance inFlash.  



By assigning an instance name, you are doing two things.  First you areassigning the name propertyof that instance to that of the instance name provided.  Secondly you arecreating a variable in the timeline with the name of the instance name that,when the instance is present in the timeline, references that instance;otherwise its value is

null,



With ActionScript 3 you cannot change the name property of a display object if it was placed on the timeline in Flash (you canif the object was dynamically created).  Even if you could, it would not changethe variable used to reference that instance as they are not connected like they were in ActionScript 2.

As with ActionScript before, all display objects have a common set ofproperties that describe their presentation on the screen in Flash.  Thisincludes properties for location, size, and color etc. In ActionScript 3, however,many of these properties' names have changed.  More specifically, theunderscores used to preceded these properties are no more.  The x and ypositions are no longer represented with _x and _y but simply x and y. Similarly, the values to which some of these properties can be defined has alsochanged.  The scaleX, scaleY, and alpha properties, for example, no longerrange from 0-100 but now 0-1 (scale properties still able to go beyond thesebounds).  This may be something to watch for if you are porting code from olderversions of ActionScript to ActionScript 3.  Not only have the property nameschanged, but also their potential values.

// ActionScript 2
myMovie._x = 50; // x position of 50
myMovie._xscale = 200; // 200% horizontal scale
myMovie._alpha = 50; // 50% transparent
// ...

// ActionScript 3 
myMovie.x = 50; // x position of 50
myMovie.scaleX = 2; // 200% horizontal scale 
myMovie.alpha = .5; // 50% transparent
// ...


Common MovieClip Property Names
ActionScript 2ActionScript 3
_namename
_xx
_yy
_widthwidth
_heightheight
_xscale (0-100)scaleX (0-1)
_yscale (0-100)scaleY (0-1)
_rotationrotation
_alpha (0-100)alpha (0-1)
_visiblevisible
_xmousemouseX
_ymousemouseY
_parentparent

Understanding the Class Inheritance Hierarchy

If you look a the documentation for a class like MovieClip (flash.display.MovieClip), you may notice that there may not be many definitions specific to the MovieClip class. There are only 9 properties and 10 methods. You won't see definitions for properties like name, x, y, etc. unless you click the "Show Inherited Public Properties/Methods" link at the top of those lists (these links appear as "Hide Inherited Public Properties/Methods" if the definitions are already visible) since those properties are not unique to the MovieClip class. Sure, the MovieClip class has those properties, but these properties are actually defined within another class and MovieClip inherits them to use as its own.

Since many classes use the same properties and methods in the same way - MovieClip, Sprite, and Shape, for example, each have x, and y properties and use them the same way - classes can be defined to reduce redundancy by having similar members defined in a single location (class) and then referencing those definitions for their own use. This is handled through what is known as class inheritance. A single base class (a.k.a. superclass) has definitions which may derived classes (a.k.a. subclasses) can use as their own as long as they do what is called extending that class.

It's an OOP concept, and though you may not be working specifically with OOP when you're programming ActionScript yourself, this is in particular is helpful to understand because the documentation is set up to reflect this association. And the documentation is something you should be very dependant on, especially when you first start learning ActionScript (or any language for that matter).

MovieClip > Sprite > DisplayObjectContainer > InteractiveObject > DisplayObject > EventDispatcher > Object

This shows that MovieClip inherits from Sprite and has all the properties and methods that Sprite does, which itself, has all the properties and methods that DisplayObjectContainer has... and so on and so forth until you reach the base class Object, the class from which all other classes in ActionScript inherit. This path shows just how similar MovieClips and Sprites really are. They are practically the same except for the extra frame-related properties and methods that are uniquely defined for MovieClip. Similarly all other objects that are visible on the screen will inherit from DisplayObject. And you know all the properties defined within the DisplayObject class will be within any display object instance that can be made visible on the stage in Flash. So don't be surprised when you click on the x link to look up the documentation of the x property and find yourself on th DisplayObject documentation page. They are one in the same.

The Display List

The display list in ActionScript 3 represents the visual hierarchy of objects or display objects on screen within the Flash player. This starts with a top-most, always present display object known as the stage followed by the main timeline, or root, and all children within the main timeline as well as their children.


Usually when referring to the display list, you're referring to the entire display list within the Flash player. This is also sometimes called the active display list. However, you may also see the term display list used with individual display object containers - display objects of the type DisplayObjectContainer (flash.display.DisplayObjectContainer) which can contain other display objects (e.g. Sprite and MovieClip instances) - where each container is said to have their own display list. The display list of any container consists of all of that container's child display objects and all children within those children.

In AVM1 with ActionScript 2, it was not possible for a movie clip instance to exist without it being attached to some timeline and effectively being a part of the SWF's display list. AVM2 and ActionScript 3 has changed that. Now it is possible for display objects to exist within memory but not be associated with any particular timeline and therefore not be on screen. When this is the case, these instances are said to be off-list as they are not within the active display list and can only be referenced through ActionScript.


Also different with ActionScript 3 is how arrangement, or depth, is handled. Before, all movie clips could be placed in just about any depth where movie clips on the timeline actually initially existed in negative depths. Now all depths start at 0 and are contiguous. In other words, if you have 2 display objects within a container, they will have to exist at depths 0 and 1. You cannot have one at depth 0 and the other at 10 leaving 9 empty depths. This ultimately could mean less flexibility, but at the same time there is a more concise understanding of the arrangement of display objects within the display list since, if you know there are 5 objects in a display object container, you know at exactly which depths they occupy. 


The root and stage

The main timeline of a FLA or SWF is known as the root timeline. In ActionScript 2, you could access this timeline directly using the _root property. This reference continues to exist in ActionScript 3, but is now called root.

ActionScript 2 also had a class or object called Stage. Using the Stage object you could determine the size of the Flash player window, check to see when that size has changed, and specify how content was affected when it did change, etc. The concept of the stage also exists in ActionScript 3, but now it has taken a new form; instead of just being just an object or static class, the stage is an instance that also acts as the top-most display object in the display list hierarchy. It is the Flash player container for which all other SWF content on screen. Whereas in ActionScript 2, the main timeline was the top-most display object in the display list, in ActionScript 3 it is the stage instance followed by the main timeline (root) as its first child. As with the root property, you can access the stage instance in ActionScript 3 using the stage property (note the lowercase "s").

There are 2 very important qualities about root and stage that you need to come to terms with when working with ActionScript 3:

  1. The root and stage properties in ActionScript 3 are not global. These properties are instead properties of the DisplayObject class, so only display object instances have access to them. Other classes, namely custom classes, do not have access unless explicity given access by another class that does.
  2. The root and stage properties will be null unless the display object from which they are being referenced is a child of the active display list. Off-list display objects will not be able to access the root timeline and the stage through those respective properties.

Dynamic display objects

When you created dynamic movie clips in ActionScript 2, you had basically 3 options: you could duplicate an existing movie clip on the screen using duplicateMovieClip(), you could create an empty movie from scratch with createEmptyMovieClip(), or you could create an instance of a movie clip symbol within your library through attachMovie().

In ActionScript 3, it's all much simpler. All you need to do to make a dynamic movie clip is create a new instance of it like you would any other class using the new keyword. For new empty MovieClip instances, you just use new MovieClip(), or better yet, new Sprite() since empty MovieClip instances cannot be given new frames (and a Sprite is just a MovieClip with no frames).

// Create a new, dynamic, off-list Sprite
var mySprite:Sprite = new Sprite();

Because display objects in ActionScript 3 can exist off screen (off-list) creating a new dynamic instance in this manner does not mean that the new instance is visible on the screen. Before it can be made visible, it has to be added to the active display list. To do that you use one of two methods: addChild() which adds a display object at the end of a display list, or addChildAt() which adds a display object at a specific location within a display list.


New Sprite instances, however, contain no content by default. Even though the above example places a Sprite on the screen, it cannot be seen because there's nothing in it to see. It's more likely that in Flash you would want to dynamically create an instance of a movie clip symbol in your FLA's library. Before you can create an instance of a movie clip that exists as a symbol in your library, then you first need to give that symbol a class name within the Linkage dialog (right or command-click on that symbol and select Linkage...) for that symbol.

In the Linkage dialog, you have a number of checkboxes providing you with some options for your symbol. Under Linkage you have two primary options, Export for ActionScript and Import for runtime sharing. Import for runtime sharing allows you to load in symbols from external SWFs. For dynamic movie clips, you need to Export for ActionScript. Additional options related to Export for ActionScript include Export for runtime sharing which is related to Import for runtime sharing, and Export in first frame which assures that your SWF will load this movie clip into the Flash Player prior to any of your frame scripts making it available for dynamic creation at any time.

Once Export for ActionScript is selected, two text fields within the Linkage dialog appear, Class and Base class. The Class field is the most important for creating dynamic movie clip instances from your library. It is needed to uniquely identify the symbol with a class (which will be automatically generated for you behind the scenes) so that it can be created with the new keyword. The Base class identifies the class from which the symbol inherits. Since you're working with movie clip symbols, this will usually be MovieClip, the default value. When working with custom classes, you could decide to put your own class as the Base class.




Bitmaps

Bitmap images are another form of media that can exist within your Flash library. They too can be created and added to the screen dynamically with ActionScript 3. The images as they exist in the library, however, do not directly translate to on-screen display objects like movie clips do. Instead, images in the library represent BitmapData (flash.display.BitmapData) instances in ActionScript. To be visible on the screen, this data needs to be given to a Bitmap (flash.display.Bitmap) instance.

As with movie clips in the library, when creating bitmap images dynamically from your library in Flash, you need to assign it a unique class name through the Linkage dialog. This is done the same way it is done with movie clips only the base class for bitmaps will be BitmapData as opposed to MovieClip.

When you create new BitmapData instances with ActionScript, it is required that both a width and height be supplied for the new instance as it defines the size for that instance. Since images in your library are based off of the BitmapData class, they too require that width and height be set for them when new instances are created. Images in your library, however, already exist with a set width and height so the values used when creating new instances of your library bitmap image do nothing. If you know the image size you should use that; otherwise 0 works fine.

Once you create an instance of your library image, you need to give it to a Bitmap instance before it can be seen on the screen. This can be done through the Bitmap constructor or by setting the bitmapData property of the Bitmap instance. That Bitmap instance would then need to be added to the active display list before it could be made visible in Flash.

Removing

Once you have a display object on the screen, and it's time for it to be removed, you can use one of two removal methods, removeChild() which removes the specified display object from a display list, and removeChildAt() which removes the child at a specific location within a display list.


There are two important points to display object removal in ActionScript 3:

  • Removal is handled through the parent display object container, not the child as was the case with removeMovieClip() in ActionScript 2 (parentObject.removeChild(childObject) vs. childObject.removeMovieClip()).
  • Removing a display object from a display list does not destroy the display object or remove it from Flash's memory. As long as you still have a reference to a display object, it will still exist off-list even if not as a child of any other timeline or container.
The latter is especially important for ActionScript 2 veterns who are used to having movie clip references become invalid after the object has been removed from the screen. This is no longer true in ActionScript 3 since the instance remains valid, just off-list. To truely get rid of the instance, all references would need to be deleted (if dynamic) or set to null, just like any other kind of non-display object in ActionScript.

The first point should also not be seen as unimportant. This is a reflection of how you should be working with ActionScript - starting at the outside looking in as opposed to being on the inside looking out, which has become a popular approach for many ActionScript 2 developers, espcially commands like removeMovieClip which work from the child removing itself from it's own parent. What this basically means is that when working with movie clips and timelines, the best approach when writing script is not to be within a movie clip referencing data and methods that exist outside that movie clip, but instead to be looking into child movie clips and letting parent containers look into that movie clip. The less dependancy any movie clip or timeline has on objects outside of itself, the better.


这些教材写的非常好!·

文章来源:http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=2


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值