2014-03-13 90 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例如更有意义的名称你的精灵。