问题
there's a large ammount of display objects in flash.display
package. It's not clear for me in what situation I should use Shape
, Sprite
or MovieClip
. What is the pro and contras in using each of them?
Thank you in advance!!
回答1:
- Shape is the simplest display object you can add on stage. It is the most limited one: you can't add childen to it (does not extend DisplayObjectContainer), does not have interactivity (does not extend InteractiveObject), does not have a timeline
- Sprite extends DisplayObjectContainer and InteractiveObject, therefore it's interactive and you can add children to it. It's the most useful display class in my opinion, as long as you don't need a timeline.
- MovieClip extends Sprite, so all of the above are true and you also get methods/properties associated with timeline control, but note that it's a dynamic class, so you can do some hacky thing on the fly, but you'll lose speed.
In short, stick to Sprite in most cases, unless you need to integrate with MovieClips from Flash Authoring. Shape is handy to quickly draw into and it's 'lighter' than Sprite, but not very flexible since you can't nest other elements to it.
回答2:
You should always use the lightest component depending on what you need:
Shape
is the one with the least possibilities. Use it when you only want aDisplayObject
withgraphics
, and no mouse interaction.Sprite
is the parent class of quite everything you need. Since it is aDisplayObjectContainer
, you can use it as a basic container for other components. You can also catch mouse events on this one.MovieClip
is aSprite
with the ability to use frames. Only use it for frame-by-frame animation (Flash style).
来源:https://stackoverflow.com/questions/10192923/shape-sprite-movieclip-and-other-display-objects-when-to-use