滚动背景图像切成两半 - cocos2d

时间:2014-01-18 05:58:21

标签: ios iphone objective-c cocos2d-iphone

我制作的游戏背景图片太大,无法呈现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;
    }
}

这似乎不起作用,我得到了奇怪的结果...任何帮助表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:1)

可能是这样:

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,{{1 }到1,sprite2(复制)到2,sprite(复制)到3。

在第二个for循环中:

sprite2

<强>更新

[backgroundNode addChild:sprite z:i tag:i + 2]; [backgroundNode addChild:sprite2 z:i tag:i + 2]; 方法中更改此内容:

-updateRunning:

到此:

pos.x += (sprite.contentSize.width * 2) - 2;

因为你有四个这样的精灵:

[1] [2] [3] [4]

当[1]移动到屏幕外时,你需要一直移动[1] [4],这样它们就像这样

[2] [3] [4] [1]

这就是为什么你需要向右移动[1]四个长度而不是向右两个长度。

<强>更新

看起来你没有足够的speedFactors。看起来你正在为speedFactors数组添加一个值,但后来尝试获取其中的两个。在添加视差滚动之前,您不需要不同的速度因子,所以现在更改:

pos.x += (sprite.contentSize.width * 4) - 1;

NSNumber* factor = [speedFactors objectAtIndex:sprite.zOrder];