【问题标题】:Using Cocos2D scheduleUpdate to delay loading使用 Cocos2D scheduleUpdate 延迟加载
【发布时间】:2012-06-01 15:08:56
【问题描述】:

我有一个算法需要几秒钟来加载一些东西,我想在实际加载开始之前首先将标签上的字符串设置为“正在加载”。这都是在同一层,这不是在场景之间切换。

我以为我可以简单地这样做:

-(void)startLoading{

[self unscheduleAllSelectors];//just in case the update is already scheduled

[self.loadingLabel setString:@"Loading...."];

[self scheduleUpdate];

 }

然后,我有这个:

-(void)update:(ccTime)delta{
[self unscheduleUpdate];
[self beginLoading];//another method that loads all the stuff

 }

我的理解是我的方法beginLoading 不应该运行到下一帧。因此我的标签应该得到正确更新。然而,这并没有发生。在加载我的所有资产并且我的标签在加载开始之前从未更新过时,会有轻微的冻结。

我错过了一步吗?

【问题讨论】:

  • 您是从场景/图层中的初始化函数调用 startLoading 吗?这个线程似乎有一个解决方案...cocos2d-iphone.org/forum/topic/15998
  • 该线程指的是不同的问题。我的更新正在运行,但它没有等待一帧,或者如果是,前一帧在新帧开始之前没有完成,我认为这是不可能的。我不是从 init 方法调用。

标签: iphone objective-c ipad cocos2d-iphone


【解决方案1】:

不,肯定不会遗漏任何东西。我停止了战斗,现在使用这种“延迟”任务弹射器。它应该确保你会在从第一个到第二个刻度的过渡中获得平局:

-(void) startLoading{
    _loadTicker=0; // an NSUInteger iVar declared in the .h
    [self schedule:@selector(tickOffLoading:)];
}

-(void) tickOffLoading:(ccTime) dt{

    _loadTicker++;
    if(_loadTicker==1) {
        [self.loadingLabel setString:@"Loading...."];
    } else {
        [self unschedule:@selector(tickOffLoading:)];
        [self beginLoading];
    }
}

【讨论】:

  • 谢谢,你和我有类似的想法。我最终仍然使用常规的update 方法,但我也在使用帧计数器 ivar
  • @andrewx :大声笑,并不感到惊讶,类似的问题会产生类似的解决方案。这很好地为加载后台任务奠定了基础,因为您可以将代码检查级联到 1、2、3 等...
  • 我仍然想知道为什么它最初不能按预期工作。我猜更新必须在最初计划时在同一个“框架”或运行循环上运行。
猜你喜欢
  • 1970-01-01
  • 2011-02-02
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
相关资源
最近更新 更多