【问题标题】:Scrolling background image cut in half - cocos2d滚动背景图片切成两半 - cocos2d
【发布时间】:2014-01-18 05:58:20
【问题描述】:

我正在制作一个游戏,它的背景图像太大,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;
    }
}

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

【问题讨论】:

    标签: ios iphone objective-c cocos2d-iphone


    【解决方案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,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 似乎不够。看起来您正在向 speedFactors 数组添加一个值,但后来尝试获取其中两个。在添加视差滚动之前,您不需要不同的速度因子,所以现在更改:

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

    NSNumber* factor = [NSNumber numberWithFloat:1.0f];
    

    【讨论】:

    • 这几乎成功了,但我仍然得到一些时髦的输出......我已经更新了我的问题,我认为这是导致奇怪输出的原因......
    • 两个背景都在加载,但每次背景循环时它们被放置在不同的位置。 bg 没有以“一个接一个”的方式放置
    • 这使它适用于第一个循环,但在那之后,它每次都回到被放置在不同的位置......我更新了我的问题中的代码以匹配我所拥有的我的游戏。
    • 好的,再次更新。你能告诉我你在修复后看到了什么吗?
    • 它工作了 2 个循环,然后一切又搞砸了:图像开始相互重叠,它们出现乱序,在第 2 个循环之后有间隙(黑屏)。
    猜你喜欢
    • 2015-08-30
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    相关资源
    最近更新 更多