Actionscript 3.0:为什么将用于移动对象的代码与对象本身分离是个好主意(例如 Ball 和 Ball Mind) [英] 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)
问题描述
我的问题几乎在标题中,为什么我一直在 actionscript 3.0 中读到在编写代码时将思想"与对象"分开是个好主意?
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.
推荐答案
如果你问为什么图形与定位、运动和物理分离;以我画的这棵树为例:
If you're asking why graphics are separated from the positioning, movement and physics; take this tree I've drawn:
在树中你会看到 Entity
有两个属性:
In the tree you'll see that Entity
has two properties:
- 图形 - 实体应该是什么样子.
- 身体 - 实体应该在哪里.
向下移动,您会看到有几项扩展了 Entity
- 最值得注意的是 Player
和 Enemy
类.
Moving down, you will see that there are several things that extend Entity
- most notable are the Player
and the Enemy
classes.
扩展上面的 Entity
类,我可以轻松更改应该用作 graphics
的内容,并且还提供略有不同的 body
.例如,玩家和敌人将有明显不同的外观,并且 Tree
类不需要使用处理速度等值的 Body
,因为它不需要移动.
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:
- 我们可以创建没有图形的实体,从而节省性能和内存.
- 如果您使用
Entity
类扩展了MovieClip
,我们可以使用不同类型的图形,而不必坚持使用MovieClip
.莉> - 我们可以在
Graphics
类中添加额外的逻辑,例如能够轻松地将 Sprite 或 MovieClip 转换为 Sprite 表以获得更好的性能. - 图形将更易于管理且更轻量级(与自动绑定到每个实体相反).
- 无需了解图形即可更轻松地处理物理.
Body
可以在不立即影响图形的情况下更新.- 您对与外观完全无关的物理学的理解将显着提高.
- 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.
这篇关于Actionscript 3.0:为什么将用于移动对象的代码与对象本身分离是个好主意(例如 Ball 和 Ball Mind)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!