Maj*_*ter 4 touch dealloc cocos2d-iphone ios
我有cocos2d的问题.我做了一个接受触摸的课程.类的子类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;
}
Run Code Online (Sandbox Code Playgroud)
所以一切都很简单.frame,size并且origin是类变量,但这现在无关紧要.所以我注册了我的班级女巫touchDispatcher,让我可以处理.触摸处理完成如下:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
//Some touch logic which i need.
}
Run Code Online (Sandbox Code Playgroud)
在dealloc我发布所有保留的信息并取消注册touchDispatcher.但从dealloc未被称为.如果我不与注册touchDispatcher的dealloc正确调用.如果重要的话,这个类作为子类添加到另一个CCLayer子类中,并且在该类中dealloc我释放了这个类.
我错过了什么?
澄清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];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1293 次 |
| 最近记录: |