【问题标题】:what to do with timers when replacing a scene?替换场景时如何处理计时器?
【发布时间】:2011-12-17 10:43:30
【问题描述】:

读了这么多之后,我可以看到在更换场景时我应该怎么做有很多想念的理解。 在 dealloc 方法上,我是否必须释放我所有的计时器?还是 cocos2d 呢??

我在启动时有 3 个计时器:

[self schedule: @selector(contact:)];
[self schedule:@selector(tick:)]; 
[self schedule: @selector(randomsActions:) interval:0.1];

在我将场景替换为菜单(replaceScene)之前,我会这样做:

[self unschedule:@selector(randomsActions:)] ; //stop timer
    [self unschedule:@selector(contact:)] ; //stop timer
    [self unschedule:@selector(contact:)] ; //stop timer

但它崩溃了。(我见过有人说我不必取消他们的计划?)

所以我在我的 dealloc 中尝试了这个:

[[CCScheduler sharedScheduler] unscheduleAllSelectorsForTarget: self];

第二次也崩溃(在课堂上崩溃:CCScheduler.m)

在我的 dealloc 中我也放了这个:(我需要吗???)

删除_contactListener;
[自我删除AllChildrenWithCleanup: 是];
删除世界;
世界 = NULL;
[超级释放];

这里发生了什么? 有人可以告诉我在替换场景时应该在 dealloc 中做什么吗? 我应该怎么处理我的计时器???

谢谢。

【问题讨论】:

    标签: cocos2d-iphone


    【解决方案1】:

    当您替换场景时,COCOS2d 会自动解除分配您为特定场景或图层启动的所有调度程序。这就是你写 [self scheduler:...]; 的原因。 Cocos2d Scheduler 与 NSTimer 有点不同。

    你不需要释放调度器,保持空白,不用担心,

    - (void)dealloc {
    
        //memory deallocation  
    [super dealloc];
    }
    

    【讨论】:

    • 我的主要问题是当我试图从菜单返回游戏场景时它崩溃了。它在 CCScheduler.m 类中崩溃:-(void) update: (ccTime) dt 方法。这是为什么 ?它与计时器有关吗?
    • CCScheduler 也被 cocos2d 的内部机制所使用。比如触摸事件等。确保你在其他部分没有做错事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2012-02-28
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多