2
iPhone 4 및 iPhone 5에서 모두 작동하는 가로 시차 스크롤을 구현하려고합니다. 폭이 1136px 인 (HD) 스프라이트에서 시작하여 사용할 수 있다고 생각했습니다. 아이폰 4도 마찬가지다. 문제는 더 이상 iPhone 4에서 작동하지 않는다는 것입니다. iPhone 5를 사용하는 경우 스크린 크기와 스프라이트 크기가 동일합니다. iPhone 4에서는 1136 픽셀 가로 모션 (즉, 스프라이트/iPhone 5의 화면 길이)에 도달 한 후에 스프라이트를 어색하게 교체하게됩니다.iPhone 4 및 5 화면에서 시차 스크롤
화면 크기/스프라이트 크기 비율에 관계없이 끝없는 시차 스크롤을 어떻게 구현할 수 있습니까?
@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;
}
}
: 여기
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;
}
는 상황입니다 : 여기
은 (Itterheim에 의해 새로운적인 Cocos2D이 책의 코드를 기반으로) 무한히에게 갈 수 있도록 스프라이트를 업데이트하는 코드