2

我正在尝试实现适用于 iPhone 4 和新 iPhone 5 的横向视差滚动。我从一个宽度为 1136 像素 (HD) 的精灵开始,并认为我也可以在 iPhone 4 上使用它。问题是它不再适用于 iPhone 4。如果您使用的是 iPhone 5,则屏幕尺寸和精灵尺寸是相同的。在 iPhone 4 上不是这样,这将导致在您达到 1136px 横向运动(即精灵/iPhone 5 屏幕的长度)后尴尬地更换精灵。

如何实现与屏幕大小/精灵大小比率无关的无限视差滚动?

这是更新精灵的代码,以便它们无限地运行(基于 Itterheim 的新 Cocos2D 2 书的代码):

for (CCSprite* sprite in spriteBatch.children)
{
    NSNumber* factor = [speedFactors objectAtIndex:sprite.zOrder];

    CGPoint pos = sprite.position;
    pos.x -= (scrollSpeed * factor.floatValue) * (delta * 50);

    // Reposition stripes when they're out of bounds
    CGSize screenSize = [CCDirector sharedDirector].winSize;
    if (pos.x < -screenSize.width)
    {
        pos.x += (screenSize.width * 2) - 2;
    }

    sprite.position = pos;
}

这是它的背景:

 @implementation ParallaxBackground

-(id) init
{
    if ((self = [super init]))
    {
        CGSize screenSize = [[CCDirector sharedDirector] winSize];

        // Get the game's texture atlas texture by adding it. Since it's added already it will simply return 
        // the CCTexture2D associated with the texture atlas.
        CCTexture2D* gameArtTexture = [[CCTextureCache sharedTextureCache] addImage:@"game-art.pvr.ccz"];

        // Create the background spritebatch
        spriteBatch = [CCSpriteBatchNode batchNodeWithTexture:gameArtTexture];
        [self addChild:spriteBatch];

        bgLayerTotal = 3;

        // Add the 6 different layer objects and position them on the screen
        for (int i = 0; i < bgLayerTotal; i++)
        {
            NSString* frameName = [NSString stringWithFormat:@"bg%i.png", i];
            CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName];
            sprite.anchorPoint = CGPointMake(0, 0.5f);
            sprite.position = CGPointMake(0, screenSize.height / 2);
            [spriteBatch addChild:sprite z:i];
        }

        // Add 7 more stripes, flip them and position them next to their neighbor stripe
        for (int i = 0; i < bgLayerTotal; i++)
        {
            NSString* frameName = [NSString stringWithFormat:@"bg%i.png", i];
            CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName];

            // Position the new sprite one screen width to the right
            sprite.anchorPoint = CGPointMake(0, 0.5f);

            sprite.position = CGPointMake(screenSize.width - 1, screenSize.height / 2);

            // Flip the sprite so that it aligns perfectly with its neighbor
            sprite.flipX = YES;

            // Add the sprite using the same tag offset by numStripes
            [spriteBatch addChild:sprite z:i tag:i + bgLayerTotal];
        }

        // Initialize the array that contains the scroll factors for individual stripes.
        speedFactors = [NSMutableArray arrayWithCapacity:bgLayerTotal];
        [speedFactors addObject:[NSNumber numberWithFloat:0.1f]];
        [speedFactors addObject:[NSNumber numberWithFloat:3.0f]];
        [speedFactors addObject:[NSNumber numberWithFloat:4.0f]];
        NSAssert(speedFactors.count == (unsigned int)bgLayerTotal, @"speedFactors count does not match bgLayerTotal!");

        scrollSpeed = 1.0f;

        [self scheduleUpdate];
    }
    return self;
}

-(void) update:(ccTime)delta
{
    for (CCSprite* sprite in spriteBatch.children)
    {
        NSNumber* factor = [speedFactors objectAtIndex:sprite.zOrder];

        CGPoint pos = sprite.position;
        pos.x -= (scrollSpeed * factor.floatValue) * (delta * 50);

        // Reposition stripes when they're out of bounds
        CGSize screenSize = [CCDirector sharedDirector].winSize;
        if (pos.x < -screenSize.width)
        {
            pos.x += (screenSize.width * 2) - 2;
        }

        sprite.position = pos;
    }
}
4

1 回答 1

1

而不是使用屏幕大小来重复背景,只需使用最大背景精灵的宽度(或 bg 块的宽度之和,如果它们被分解)。您也可以将最大宽度硬编码为 1136。

所以,改变:

CGSize screenSize = [CCDirector sharedDirector].winSize;
if (pos.x < -screenSize.width)
{
    pos.x += (screenSize.width * 2) - 2;
}

类似于:

CCSprite *bg = (CCSprite*)[spriteBatch getChildByTag:0];
if (pos.x < -bg.contentSize.width)
{
    pos.x += (bg.contentSize.width * 2) - 2;
}
于 2013-04-16T17:28:00.003 回答