0

我有3张图片,我想重复一遍又一遍。当图层移动到左侧时,第一个图像不再可见,并且此图像在最后一个图像右侧之后处于新位置,以便第一个图像成为第三个图像,并重复动画。如何避免重复图像时的时间流逝?

我的问题是,有一点点时间流逝有时,我认为它发生时,第一个图像被删除,并把第三个位置。我们几乎没有注意到它,但它有点“暂停”动画0.1秒,我想避免这种情况。

你知道吗?我已经试过这段代码(删除图片并再次插入),或者只是用replace...atIndex替换,但我遇到了同样的问题。代码是:

if (offsetParam <= offset1-spriteWidth){ 
     CCSprite *temp = [[sprites objectAtIndex:0] retain]; 
     [sprites removeObjectAtIndex:0]; 
     [sprites addObject:temp]; 
     temp.position = ccp(-offset1+(2*spriteWidth), temp.position.y); 
     [temp release]; 

     offset1 -= spriteWidth; 
} 

任何想法?

感谢


编辑: 用计数器,对象不动? (我曾试图用“保留”以防万一,但有或没有它,我已经得到了同样的结果)

if (rightDirection){ 
    //RIGHT 
    CCLOG(@"offsetParam %f, offset1-spriteWidth %f", offsetParam, offset1-spriteWidth); 
    if (offsetParam < offset1-spriteWidth){ //move the block to the right 

     CCSprite *temp = [[grasses objectAtIndex:counterGrass] retain]; 
     counterGrass++; 
     if (counterGrass>2) counterGrass = 0; 
     temp.position = ccp(offset1+(2*spriteWidth), temp.position.y);//does not work 
     [temp release]; 
     offset1 -= spriteWidth; 
    } 

回答

1

的类型是CCArray或NSMutableArray里的“精灵”?由于CCArray在removeObjectAtIndex方面存在严重的性能问题,特别是在索引较低的情况下(在数组的开头),与您的情况一样。

你可以尝试切换到NSMutableArray,看看是否有帮助。

另外,避免使用删除和添加,因为它是多余的。而是使用索引计数器来告诉你哪个数组索引是第一个精灵。当索引> = sprite数组中的项目数时,将其重置为0.

+0

@ LearnCocos2D:我总是想知道:什么是CCArray对于无论如何都有用?我从来没有使用它(只有NSMutableArray) – Voldemort 2012-08-13 20:51:45

+0

@ LearnCocos2D感谢它的工作正常,感谢您的帮助! – Paul 2012-08-16 07:14:11