在cocos2d + box2d中设置游戏对象 [英] Game Objects setup in cocos2d + box2d
问题描述
我遇到一个设计问题,这可能是由于我尚未完全了解cocos2d和box2d。
I am having a design issue, this may be due to the fact that i don't fully understand cocos2d and box2d yet.
我想创建一个具有CCSprite(图像数据)和b2Body(物理)的游戏对象,我是否正确地制作了一个包含两者的封装对象?如果我这样做,这将使我能够更改CCSprite
I want to create game objects that have a CCSprite(image data) and a b2Body(physics), Would i be right to make an encapsulating object that contains both? if i did this this would enable me to make changes to the CCSprite
OR
在示例代码中,b2Body具有一个userData变量,该精灵被设置为该变量。然后,使用这种方法,我只需要一个指向物理对象的指针,即可处理CCSprite。
as i have seen in the example code the b2Body has a userData variable which the sprite is set as. Then with this method i would only need to have a single pointer to the physics objects which would take care of the CCSprite.
Thx预先。
推荐答案
我的首选方法是将两者封装在Actor对象中。我的Actor对象还负责根据b2Body数据更新CCSprite位置/旋转。然后,我将该Actor对象用作b2Body中的userData。迭代访问侦听器中的正文时,以Actor作为userData很有帮助。
My preferred way of doing it is to encapsulate both in an Actor object. My Actor object is also responsible for updating the CCSprite position/rotation based on the b2Body data. Then I use that Actor object as the userData in the b2Body. Having the Actor as the userData is helpful when iterating over bodies in contact listeners.
这篇关于在cocos2d + box2d中设置游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!