[英]Cocos2d touch dispatcher causing object retain
I have a problem with cocos2d. 我有cocos2d的问题。 I made a class which receives touches.
我做了一个接受触摸的课程。 Class is a subclass of
CCLayer
and init
looks like so: Class是
CCLayer
的子类, init
看起来像这样:
- (id)initWithFrame:(CGRect)frameSize
{
self = [super init];
if (self)
{
frame = frameSize;
size = frame.size;
origin = frame.origin;
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
return self;
}
So everything is kept simple. 所以一切都很简单。
frame
, size
and origin
are class variables but this does not matter right now. frame
, size
和origin
是类变量,但这现在无关紧要。 So I register my class witch touchDispatcher
which allows me to handle touches. 所以我注册了我的类巫婆
touchDispatcher
,它允许我处理触摸。 The touch handling is done like so: 触摸处理完成如下:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
//Some touch logic which i need.
}
And in dealloc
I release all the retained information and unregister from touchDispatcher
. 在
dealloc
我释放所有保留的信息并从touchDispatcher
取消注册。 But the dealloc
is never called. 但
dealloc
从未被称为。 If I dont register with touchDispatcher
the dealloc
is called properly. 如果我不注册
touchDispatcher
,则正确调用dealloc
。 If important, this class is added as a child in another CCLayer subclass and in that class's dealloc
I release this one. 如果重要的话,这个类作为子类添加到另一个CCLayer子类中,并且在该类的
dealloc
我释放了这个类。
What am I missing? 我错过了什么?
to clarify giorashc's answer, do this. 澄清giorashc的答案,做到这一点。 :
:
- (void)onEnter {
[super onEnter];
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
- (void)onExit {
// called before the object is removed from its parent
// force the director to 'flush' its hard reference to self
// therefore self's retain count will be 0 and dealloc will
// be called.
[super onExit];
[[CCDirector sharedDirector].touchDispatcher removeDelegate:self];
}
You said it yourself : the touch dispatcher is retaining your layer object as you used it as a delegate in addTargetedDelegate
. 您自己说过:触摸调度程序保留您的图层对象,因为您在
addTargetedDelegate
中将其用作委托。 So you must un-register your layer from the dispatcher from somewhere else or else the final release will never get called (so dealloc
will not be called). 因此,您必须从其他地方的调度程序中注销您的图层,否则最终版本将永远不会被调用(因此不会调用
dealloc
)。
In short : Do not un-register touch dispatcher from the dealloc method if the delegate is the same object 简而言之:如果委托是同一个对象,请不要从dealloc方法取消注册触摸调度程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.