[英]Should I use CCArray to hold custom CCNode in cocos2d-x?
I have met a problem recently. 我最近遇到了一个问题。 I used
vector
STL a lot in cocos2d-x. 我在cocos2d-x中大量使用了
vector
STL。 And in one of my class I wrote: 在我的一堂课上,我写道:
vector<StrokeDrawnode*> strokedrawList;
StrokeDrawnode
is inherited from CCNode
. StrokeDrawnode
继承自CCNode
。 However, I read some article said that it is better to use CCArray
to hold sub-class of CCObject
. 但是,我读了一些文章说,这是更好地使用
CCArray
持有子类的CCObject
。 Actually, I have problem with the memory management. 实际上,我在内存管理方面存在问题。 I guess it is the problem.
我想这是问题所在。 So my question is what situation should I use
CCArray
and How can I deal with the memory management. 所以我的问题是我应该在什么情况下使用
CCArray
以及如何处理内存管理。
In what situation should I define the class member like this? 在什么情况下我应该这样定义班级成员?
CC_SYNTHESIZE_RETAIN(CCSprite* , m_sprite_draw, Sprite);
The main difference between std::vector
and CCArray
in this case is that when you add objects to CCArray
they get retained. 在这种情况下,
std::vector
和CCArray
之间的主要区别在于,当您将对象添加到CCArray
它们将被保留。 This is really important in cocos, as CCObject
-dervied (basically everything) objects get destroyed when their retainCount
reaches 0. This is done automatically between every frame. 这在cocos中确实非常重要,因为
CCObject
-dervied(基本上是所有对象)的对象在其retainCount
达到0时会被销毁。这在每帧之间自动完成。
Consider this example : Let's say you want to create 5 sprites and cache them for later use (they don't get to be on the screen until some time later in the future). 考虑以下示例:假设您要创建5个精灵,并将其缓存以备后用(直到以后的某个时间它们才会出现在屏幕上)。 Code (somewhere in the
init()
method): 代码(
init()
方法中的某个位置):
for(int i = 0; i < 5; ++i) {
CCSprite *vectorSprite = (...);
CCSprite *arraySprite = (...);
_vector.push_back(vectorSprite);
_array->addObject(arraySprite);
}
_array->retain(); // < ------- IMPORTANT!
// end of init method
We of course assume that _vector
and _array
are instance variables and don't get destroyed when init()
ends. 我们当然假定
_vector
和_array
是实例变量,并且在init()
结束时不会被破坏。
What does happen before the next frame is drawn is that all sprites we put into the _vector
will get destroyed - the vector will hold pointers to invalid memory locations. 在绘制下一帧之前发生的事情是,我们放入
_vector
所有子_vector
都会被破坏-该向量将保存指向无效内存位置的指针。
Objects we put in _array
will not, because addObject
retains them for us. 我们把对象
_array
不会,因为addObject
保留他们对我们。 Notice that the _array
itself has to be retained as well, or it will be destroyed (I'm not sure about its contents). 注意,
_array
本身也必须保留,否则它将被销毁(我不确定其内容)。
Generally, I think it may be better to use cocos-containers
when working with cocos objects, as you only have to remember about retaining the container itself, and not all the objects. 通常,我认为在处理cocos对象时使用
cocos-containers
可能会更好,因为您只需要记住保留容器本身,而不是所有对象。 IF you really want to use std::vector
it may be plausible to subclass std::vector
so that its push_back
and pop_back
methods will retain
and realese
your objects respectively. 如果您确实要使用
std::vector
,则可以合理地将std::vector
子类化,以便其push_back
和pop_back
方法分别retain
并realese
您的对象。
As to the macro - I have never used it, but it expands to this : 至于宏-我从未使用过它,但是它扩展为:
#define CC_SYNTHESIZE_RETAIN(varType, varName, funName) \
private: varType varName; \
public: virtual varType get##funName(void) const { return varName; } \
public: virtual void set##funName(varType var) \
{ \
if (varName != var) \
{ \
CC_SAFE_RETAIN(var); \
CC_SAFE_RELEASE(varName); \
varName = var; \
} \
}
Which creates a setter and a getter for your variable for outside use. 这将为您的变量创建一个setter和一个getter供外部使用。 In my opinion it seems only viable if you want to expose a variable to outside use and have this methods provided for you automatically.
在我看来,仅当您想将变量公开给外部使用并自动为您提供此方法时,这才似乎可行。 The added value here is of course the
retain - release
process done in the setter. 当然,这里的增加值是在设置器中完成的
retain - release
过程。
Let me know if anything is not clear! 让我知道是否有任何不清楚的地方! Cheers.
干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.