私はiPhone4と新しいiPhone5の両方で機能する横向き視差スクロールを実装しようとしています。幅1136px(HD)のスプライトから始めて、iPhone4でも同じように使用できると思いました。問題は、iPhone4では動作しなくなることです。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;
}
}