我正在制作一款背景图像太大,iphone无法渲染的游戏。所以我不得不把它切成两半。由于我的背景是水平滚动的(就像在JetPack Joyride中一样),我想把它做成两半放在一起,让它看起来像一张图像。所以我试着把图像的前半部分放在屏幕的最左边,然后把第二部分放在第一部分的边缘。这就是我迄今为止所尝试的。
在init
:中
numStripes = 1;
for (NSUInteger i = 0; i < numStripes; i++)
{
CCSprite *sprite = [CCSprite spriteWithFile:@"bg0.png"];
CCSprite *sprite2 = [CCSprite spriteWithFile:@"bg1.png"];
sprite.anchorPoint = CGPointMake(0, 0.5f);
sprite.position = CGPointMake(0, screenSize.height / 2);
sprite2.anchorPoint = CGPointMake(0, 0.5f);
sprite2.position = CGPointMake(sprite.position.x + sprite.contentSize.width, screenSize.height / 2);
[backgroundNode addChild:sprite z:i tag:i];
[backgroundNode addChild:sprite2 z:i tag:i];
}
// endless scrolling
for (NSUInteger i = 0; i < numStripes; i++)
{
CCSprite *sprite = [CCSprite spriteWithFile:@"bg0.png"];
CCSprite *sprite2 = [CCSprite spriteWithFile:@"bg1.png"];
sprite.anchorPoint = CGPointMake(0, 0.5f);
sprite2.anchorPoint = CGPointMake(0, 0.5f);
sprite.position = CGPointMake(sprite.contentSize.width + sprite2.contentSize.width - 1, screenSize.height / 2);
sprite2.position = CGPointMake(sprite.contentSize.width * 2.0f + sprite2.contentSize.width - 1.0f, screenSize.height / 2);
[backgroundNode addChild:sprite z:i tag:i + 2];
[backgroundNode addChild:sprite2 z:i tag:i + 2];
}
speedFactors = [[CCArray alloc] initWithCapacity:numStripes];
[speedFactors addObject:[NSNumber numberWithFloat:1.0f]];
NSAssert([speedFactors count] == numStripes, @"speedFactors count does not match numStripes!");
这可能是问题所在:
-(void) dealloc
{
#ifndef KK_ARC_ENABLED
[speedFactors release];
[super dealloc];
#endif // KK_ARC_ENABLED
}
-(void) update:(ccTime)delta
{
if (([[GameMechanics sharedGameMechanics] gameState] == GameStateRunning) || [[GameMechanics sharedGameMechanics] gameState] == GameStateMenu)
{
[self updateRunning:delta];
}
}
- (void) updateRunning:(ccTime)delta
{
// read current scroll Speed
scrollSpeed = [[GameMechanics sharedGameMechanics] backGroundScrollSpeedX];
CCSprite* sprite;
// we use a c-array since it is faster on iteration
CCARRAY_FOREACH([backgroundNode children], sprite)
{
// retrieve the scrollspeed factor for the current sprite
NSNumber* factor = [speedFactors objectAtIndex:sprite.zOrder];
// move the background layer
CGPoint pos = sprite.position;
pos.x -= scrollSpeed * [factor floatValue] * delta;
// when a layer is off screen on the left side, move it to the right end of the screen
if (pos.x < -sprite.contentSize.width)
{
pos.x += (sprite.contentSize.width * 4) - 1;
}
sprite.position = pos;
}
}
这似乎不起作用,我得到了奇怪的结果。。。感谢您的帮助。谢谢
可能是这个吗:
sprite.position = CGPointMake(0, screenSize.height / 2);
sprite2.position = CGPointMake(sprite.position.x*1.5, screenSize.height / 2);
sprite2
的初始x值正在设置为0。你可能想要的是:
sprite2.position = CGPointMake(sprite.position.x + sprite.contentSize.width, screenSize.height / 2);
这里还有另一个问题:
sprite.position = CGPointMake(sprite.contentSize.width - 1)*2, screenSize.height / 2);
sprite2.anchorPoint = CGPointMake(0, 0.5f);
sprite2.position = CGPointMake((sprite2.contentSize.width - 1)*2, screenSize.height / 2);
可能应该是:
sprite.position = CGPointMake(sprite.contentSize.width + sprite2.contentSize.width - 1, screenSize.height / 2);
sprite2.position = CGPointMake(sprite.contentSize.width * 2.0f + sprite2.contentSize.width - 1.0f, screenSize.height / 2);
您可能还遇到了将同一个标签设置为两个不同精灵的问题。您将sprite
及其副本的标记都设置为0,将sprite2
及其副本的标签都设置为1。您应该将sprite
的标记设置为0,将sprite2
设置为1,将sprite
(副本)设置为2,并将sprite2
(副本)设为3。
在第二个for循环中:
[backgroundNode addChild:sprite z:i tag:i + 2];
[backgroundNode addChild:sprite2 z:i tag:i + 2];
更新:
在您的-updateRunning:
方法中更改此:
pos.x += (sprite.contentSize.width * 2) - 2;
到此:
pos.x += (sprite.contentSize.width * 4) - 1;
因为你有四个这样的精灵:
[1] [2][3][4]
当[1]移动到屏幕外时,你需要将[1]一直移动到[4]后面,这样它们就像这个
[2] [3][4][1]
这就是为什么你需要向右移动[1]四个长度,而不是向右移动两个长度。
更新:
看起来你的速度不够。看起来您正在向speedFactors数组添加一个值,但稍后尝试获取其中两个值。在添加视差滚动之前,您不需要不同的速度因子,所以现在更改:
NSNumber* factor = [speedFactors objectAtIndex:sprite.zOrder];
至
NSNumber* factor = [NSNumber numberWithFloat:1.0f];