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