gpt4 book ai didi

ios - Cocos2d 触摸调度程序导致对象保留

转载 作者:可可西里 更新时间:2023-11-01 05:06:17 26 4
gpt4 key购买 nike

我对 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;
}

所以一切都保持简单。 framesizeorigin 是类变量,但现在这并不重要。所以我注册了我的类女巫touchDispatcher,它允许我处理触摸。触摸处理是这样完成的:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
//Some touch logic which i need.
}

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];
}

关于ios - Cocos2d 触摸调度程序导致对象保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14022940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com