2014-03-13 165 views
0

注意:爲此,我使用一個名爲spritebuilder的程序,該程序允許我使用比通常需要的更少的代碼創建遊戲。如果你知道一個解決方案只是所有的代碼,然後通過一切手段隨時分享它:)循環一個垂直精靈目標C精靈生成器

此外,對於這個問題,我在這個鏈接跟着教程:Build Your Own Flappy Bird Clone。只需向下滾動到「Loop the Ground」部分即可。

這就是我的問題。我目前正在開發一款遊戲,並且我創建了一款相機,它可以與我創建的角色精靈垂直滾動,但是我需要一個特定的圖像來循環播放。當圖像離開屏幕的底部時,我希望它無限地循環到屏幕的頂部。爲此我創建了兩個相同的圖像(在這種情況下它是樹的樹皮)。一個將在屏幕上,而另一個將在屏幕外,因此當第一個圖像離開屏幕時,第二個圖像將無縫地替換它。我爲這些圖像創建了兩個對象,併爲它們分配了名稱_ground1和_ground2,並且我還創建了一個用於存儲它們的NSArray。(請參閱上面的鏈接,如果它有點令人困惑) 這裏是代碼我有:

CCNode *_ground1; 
CCNode *_ground2; 
NSArray *_grounds; 

    for (CCNode *ground in _grounds) { 
    // get the world position of the ground 
    CGPoint groundWorldPosition = [_physicsNode convertToWorldSpace:ground.position]; 
    // get the screen position of the ground 
    CGPoint groundScreenPosition = [self convertToNodeSpace:groundWorldPosition]; 
    // if the left corner is one complete width off the screen, move it to the right 
    if (groundScreenPosition.y <(-1 * ground.contentSize.height)) { 
     ground.position = ccp(ground.position.x , ground.position.y + 2 * ground.contentSize.height); 
    } 

由於某種原因,當我嘗試這個,它似乎沒有工作。會發生什麼情況是,相機會按照它的意圖進行垂直行進,但圖像不會循環。一旦兩張圖像離開屏幕底部,就不會有新的圖像替換它們。

回答

0

我也做了這個項目,如上述教程。它工作正常,但你有一些錯誤,在spritebuilder中設置變量。在你的上面的代碼中重新編碼代碼並嘗試它。你只能投入少於可能的問題。

if (groundScreenPosition.y <=(-1 * ground.contentSize.height)) { 
     ground.position = ccp(ground.position.x , ground.position.y + 2 * ground.contentSize.height); 
    } 
0

您正在使用CCNode對象作爲_ground1_ground2

CCNode對象通常沒有contentSize, they will return 0 unless you explicitly set them inSpriteBuilder`。

確保您在SpriteBuilder和代碼中使用CCSprite對象。

此外,作爲一個友好的提示,你也應該考慮重構(重命名)爲您的使用情況一樣_treeBark1treeBark2例如更有意義的名稱你的精靈。