Actionscript 3.0: Why is it a good idea to detach the code for moving an object from the object itself (eg. Ball and the Ball Mind)

后端 未结 1 1149
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-22 14:22

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?

相关标签:
1条回答
  • 2021-01-22 14:45

    If you're asking why graphics are separated from the positioning, movement and physics; take this tree I've drawn:

    enter image description here

    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.
    0 讨论(0)
提交回复
热议问题