Cocos2d设置显示帧异步

时间:2013-05-13 16:32:25

标签: ios multithreading cocos2d-iphone

所以我正在做一个类似以下的儿童游戏:

我有大约118种不同的 CENTER_OBJECTS ,如Apple,Ball,Cat等,它们将放置在中心, ALPHABETS 围绕它旋转,所以当你点击正确的时候你移动的字母表和 CENTER_OBJECT 被替换。

我面临的问题是当更换CENTER_OBJECT的显示框时, 我观察到移动的字母表略有滞后。我很确定通过将显示框设置为新单词Center Object is some word like APPLE , BOY and blue boxes are ALPHABETS 的显示框来导致

缓存每个 CENTER_OBJECT异步的Sprite表。那么有什么方法可以建议你停止这种轻微的延迟吗?

修改

好吧,在播放器开始播放之前,我会缓存5个中心对象,当只剩下2个缓存对象时,我开始缓存接下来的5个项目。所以我所拥有的是一个功能:

-(void)getNext5RandomsForHalf:(NSInteger)half{
int i;

// half is just a way to preload , alternatively 1st half
// or 2nd half is used for cached Center Objects 
if (half==0) {

    i=0;
}else
{
    i=5;
}

for (int j=0; j<5; j++) {

  NSInteger objectIndex;

  do {
        objectIndex=(arc4random()%[self.centerObjects count]);
  } while (playedObjects[objectIndex]==1);

  playedObjects[objectIndex]=1;

  batchRandomObjects[j+i]=objectIndex; // this is used to store precalculated 
                                       //random numbers 

  NSString *tempString = (NSString*)[self.centerObjects objectAtIndex:j];
  NSString *filename = [NSString stringWithFormat:@"%@.plist",tempString];
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:filename];
CCLOG(@"%@ file added",filename);
    }


    dispatch_async( dispatch_get_main_queue(), ^{


         // Code for Starting the play for the first time . 
         if (justStarted) {

            [self playForNextObjectWithNumberOfLetters:self.numberOfLetters];
            justStarted=NO;
        }
    });
});

}

在我的playForNextObjectWithNumberOfLetters中:我声明并初始化游戏对象:CenterObject,并相应地设置其显示框。所以我有一个名为NSString * centerObjectName的属性,我将其覆盖以显示框架用途

-(void)setCenterObjectName:(NSString*)name
{

_centerObjectName = [name retain];


        CCSpriteFrame* frame = [[CCSpriteFrameCache
sharedSpriteFrameCache]spriteFrameByName:_centerObjectName];

self.displayFrame = frame;

}

0 个答案:

没有答案