Actionscript 3.0: Why is it a good idea to detach

2019-02-25 15:34发布

问题:

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:

  1. Graphics - what the entity should look like.
  2. 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:

  1. We can create entities that don't have graphics, saving performance and memory.
  2. We can use different types of graphics rather than having to stick to MovieClip if you had extended MovieClip with your Entity class.
  3. 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.
  4. The graphics will be easier to manage and more lightweight (as opposed to if it were auto-bundled with each entity).
  5. Physics will be easier to deal with without needing to know about graphics.
  6. The Body can be updated without immediate effects on the graphics.
  7. Your understanding of physics being completely unrelated to appearance will improve significantly.