0

注意:为此,我使用了一个名为 spritebuilder 的程序,它允许我使用比通常需要的更少的代码来创建游戏。如果您知道一个全是代码的解决方案,那么请随时分享它:)

另外,对于这个问题,我在这个链接上遵循了一个教程:构建你自己的 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);
    }

出于某种原因,当我尝试这个时,它似乎不起作用。发生的情况是,相机将按照预期的方式垂直移动,但图像不会循环。一旦这两个图像离开屏幕底部,就不会有新的图像取代它们。

4

2 回答 2

0

您将CCNode对象用作_ground1_ground2

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

确保您在代码中使用CCSprite对象。SpriteBuilder

此外,作为一个友好的提示,您还应该考虑使用更有意义的名称重构(重命名)您的精灵,例如_treeBark1treeBark2

于 2014-03-13T19:35:26.857 回答
0

我也按照上面的教程完成了这个项目。它工作正常,但你在 spritebuilder 中设置变量时有一些错误。在您上面的代码中将代码替换为并尝试一下。你只放少于可能是它的问题。

 if (groundScreenPosition.y <=(-1 * ground.contentSize.height)) {
        ground.position = ccp(ground.position.x , ground.position.y + 2 * ground.contentSize.height);
    }
于 2014-03-13T05:53:52.357 回答