问题
My questioin is pretty much in the title, Why do I keep reading in actionscript 3.0 that its a good idea to seperate the 'mind' from the 'object' when writing code?
Thanks for any help, this is confusing the hell out of me.
回答1:
If you're asking why graphics are separated from the positioning, movement and physics; take this tree I've drawn:

In the tree you'll see that Entity
has two properties:
- Graphics - what the entity should look like.
- Body - where the entity should be.
Moving down, you will see that there are several things that extend Entity
- most notable are the Player
and the Enemy
classes.
Extending my Entity
class above, I can easily change what should be used as the graphics
and also provide slightly different bodies
. For example, the player and enemies will have obviously different appearances, and the Tree
class won't need to use a Body
that deals with values like velocity because it doesn't move.
Here are some advantages of the above:
- We can create entities that don't have graphics, saving performance and memory.
- We can use different types of graphics rather than having to stick to
MovieClip
if you had extendedMovieClip
with yourEntity
class. - We can add additional logic into the
Graphics
class such as being able to easily covert a Sprite or MovieClip into a sprite sheet for better performance. - The graphics will be easier to manage and more lightweight (as opposed to if it were auto-bundled with each entity).
- Physics will be easier to deal with without needing to know about graphics.
- The
Body
can be updated without immediate effects on the graphics. - Your understanding of physics being completely unrelated to appearance will improve significantly.
来源:https://stackoverflow.com/questions/9611426/actionscript-3-0-why-is-it-a-good-idea-to-detach-the-code-for-moving-an-object