注意:为此,我使用了一个名为 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);
}
出于某种原因,当我尝试这个时,它似乎不起作用。发生的情况是,相机将按照预期的方式垂直移动,但图像不会循环。一旦这两个图像离开屏幕底部,就不会有新的图像取代它们。