2013-08-28 1 views
0

나는 첫 번째 튜토리얼을 개발 중이고 한 가지 문제에 직면하고있다. 내 문제는 이미지 (1024 X 320)이고 내 방향은 내가 필요로하는 풍경이다. 이 목적을 위해 이미지를 오른쪽에서 왼쪽으로 계속 이동하려면 Ray (우주선 덕분에)가 우주 사수 튜토리얼을 사용했지만 이미지가 계속 나타나지 않는 것 같습니다. 내 코드입니다 .. 스프라이트 (Parallax)에서 무한 스크롤

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

     // 1) Create the CCParallaxNode 
     backgroundNode = [CCParallaxNode node]; 
     [self addChild:backgroundNode z:-1]; 

     // 2) Create the sprites we'll add to the CCParallaxNode 
     Back = [CCSprite spriteWithFile:@"bg_front_spacedust.png"]; 
     //Back.position=ccp(screenSize.width/2, screenSize.height/2); 
     Back.rotation = -90; 
     Back1 = [CCSprite spriteWithFile:@"bg_front_spacedust.png"]; 
     Back1.rotation = -90; 

     // 3) Determine relative movement speeds for space dust and background 
     CGPoint dustSpeed = ccp(0.1, 0.1); 

     // 4) Add children to CCParallaxNode 
     [backgroundNode addChild:Back z:0 parallaxRatio:dustSpeed positionOffset:ccp(screenSize.width/2, screenSize.height/2)]; 
     NSLog(@"back.content width is...%f",Back.contentSize.width); 
     [backgroundNode addChild:Back1 z:1 parallaxRatio:dustSpeed positionOffset:ccp(screenSize.width/2, screenSize.height*2)]; 

     // 5) Enable updates 
     [self scheduleUpdate]; 
} 
    return self; 
} 


- (void)update:(ccTime)dt { 

    // 1) Update background position 
    CGPoint backgroundScrollVel = ccp(0,-1000); 
    backgroundNode.position = ccpAdd(backgroundNode.position, ccpMult(backgroundScrollVel, dt)); 

    // 2) Check for background elements moving offscreen 
    NSArray *spaceDusts = [NSArray arrayWithObjects:Back, Back1, nil]; 
    for (CCSprite *spaceDust in spaceDusts) { 
     if ([backgroundNode convertToWorldSpace:spaceDust.position].x < -spaceDust.contentSize.width) { 
      [backgroundNode incrementOffset:ccp(2*spaceDust.contentSize.width,0) forChild:spaceDust]; 
     } 
    } 
} 

사전에이
감사에서 저를 도와주세요.

답변

1

이 하나

if (backgroundNode.position.y <-screenSize.height*2) 
      backgroundNode.position = ccp(0,0); 

하려고 만하면 다시 업데이트에서 0으로 backgroundNode의 위치를 ​​설정해야 한 번만 수행됩니다하고있는 접근 한 번 방법. 여기에있는 배수는 다를 수 있습니다.

0

가 paraNode하는 AddChild 라인이 CGPointMake (1.0,0) 위에서 변화 CGPointMake (0, 1.0) 아래에서 이동하는 paralax를 만드는 코드를보십시오. init 메소드가 호출 될 때

-(id) init 
{ 
// always call "super" init 
// Apple recommends to re-assign "self" with the "super's" return value 
if((self=[super init])) 
{ 
    float yPos =0.0; 
    NSMutableString *fileNameString = [[NSMutableString alloc]initWithCapacity:0]; 
    if (IS_IPHONE_5) 
    { 
     [fileNameString appendString:@"Background-568h.png"]; 
     yPos= 560.0; 
    } 
    else 
    { 
     [fileNameString appendString:@"Background.png"]; 
     yPos= 470.0; 
    } 
    background1 = [CCSprite spriteWithFile:fileNameString]; 
    background1.tag = 1; 
    background1.anchorPoint = CGPointMake(0,0); 

    background2 = [CCSprite spriteWithFile:fileNameString]; 
    background2.tag = 2; 
    background2.anchorPoint = CGPointMake(0,0); 

    background3 = [CCSprite spriteWithFile:fileNameString]; 
    background3.tag = 3; 
    background3.anchorPoint = CGPointMake(0,0); 

    background4 = [CCSprite spriteWithFile:fileNameString]; 
    background4.tag = 4; 
    background4.anchorPoint = CGPointMake(0,0); 


    paraNode = [CCParallaxNode node]; 
    [paraNode addChild:background1 z:1 parallaxRatio:CGPointMake(0, 1.0) positionOffset:CGPointMake(0, 0)]; 
    [paraNode addChild:background2 z:2 parallaxRatio:CGPointMake(0, 1.0) positionOffset:CGPointMake(0, -yPos)]; 
    [paraNode addChild:background3 z:3 parallaxRatio:CGPointMake(0, 1.0) positionOffset:CGPointMake(0, -yPos*2)]; 
    [paraNode addChild:background4 z:4 parallaxRatio:CGPointMake(0, 1.0) positionOffset:CGPointMake(0, -yPos*3)]; 



    [self addChild:paraNode z:-1 tag:123]; 
    [self updateFrameRate:0.7 andYposition:yPos]; 

    [fileNameString release]; 
    fileNameString = nil; 
} 
return self; 
} 

-(void)updateFrameRate:(float)speedValue andYposition:(float)yposToSet 
{ 

    move1 = [CCMoveBy actionWithDuration:speedValue position:CGPointMake(0, yposToSet)]; 
    move2 = [CCMoveBy actionWithDuration:0.0 position:CGPointMake(0, -yposToSet)]; 
    move3 = [CCMoveBy actionWithDuration:0.0 position:CGPointMake(0, 0)]; 
    CCSequence* sequence = [CCSequence actions:move1,move2,move3, nil]; 
    CCRepeatForever* repeat = [CCRepeatForever actionWithAction:sequence]; 
    [paraNode runAction:repeat]; 

}