2011-03-29 7 views
4

나는 Cocos2D 응용 프로그램에서 세로로 스크롤하려고하는 세 개의 이미지 (320x480)가 있습니다. 내 초기화 방법에Cocos2D 세로 스크롤 배경

, 나는 다음과 같은 한 :

-(void) scroll:(ccTime)dt 
{ 
//move 30*dt px vertically 
background.position = ccp(background.position.x, background.position.y - 30*dt); 
background2.position = ccp(background2.position.x, background.position.y - 30*dt); 

//reset offscreen position 
if (background.position.y < 290) 
{ 
    background.position = ccp(480/2, 480); 
}else if (background2.position.y < 290) 
{ 
    background2.position = ccp(480/2,480); 
} 
} 

가 현재 무슨 일이 일어나고 있는지 처음으로 배경 이미지가 약 4 분 오프셋입니다 : 여기

//adding background sprites 
background = [CCSprite spriteWithFile:@"BG1.png"]; 
background2 = [CCSprite spriteWithFile:@"BG2.png"]; 

//position background sprites 
background.position = ccp(size.width, size.height/2); 
background2.position = ccp(size.width, size.height*2); 

//schedule to move background sprites 
[self schedule:@selector(scroll:)]; 

//adding them to the main layer 
[self addChild:background z:0]; 
[self addChild:background2 z:0]; 

을 그리고 나의 스크롤 방법입니다 화면 (가로 방향)으로 이동하고 화면 아래쪽에서 4 분의 1 지점에서 시작하지만 아래로 스크롤됩니다. 내 두 번째 배경 이미지는 실제로 스폰되지 않으며 첫 번째 이미지는 오프셋되는 동안 반복해서 반복됩니다. 두 이미지를 백그라운드에서 원활하게 연속적으로 반복 할 수있는 방법이 있습니까? 어떻게 세 번째 이미지를 통합 할 수 있습니까?

또 하나의 빠른 측면 질문인데, 이름 (예 : background2/background3)에 개체 (개체라고 생각합니다) 이름을 지정하는 것이 좋지 않습니까?

+0

는 y 좌표 background2 년대처럼 나에게 보이는이 너무 높은, 그래서 화면을 시작하고이 유지됩니다. – lins314159

답변

6

가로 모드에서 가로 스크롤 테스트 (모두 가로 스크롤에서 세로 스크롤로 변경해야합니다. 알아낼 수 있어야합니다.)는 ccposition이 0이 아닌 스프라이트 중간에서 오는 것을 잊지 마십시오. 0 관점 ... :

CGSize size = [CCDirector sharedDirector].winSize; 

    //adding background sprites 
    background = [CCSprite spriteWithFile:@"tracktest.png"]; 
    background2 = [CCSprite spriteWithFile:@"tracktest.png"]; 
    [background.texture setAliasTexParameters]; 
    [background2.texture setAliasTexParameters]; 

    //position background sprites 
    background.position = ccp(background.contentSize.height/2,background.contentSize.width/2); 
    background2.position = ccp(size.width,0); 

    //schedule to move background sprites 
    [self schedule:@selector(scroll:)]; 

    //adding them to the main layer 
    [self addChild:background z:0]; 
    [self addChild:background2 z:0]; 

-scroll 방법 :

-(void) scroll:(ccTime)dt 
{ 
     //move 30*dt px vertically 
    if (background.position.x<background2.position.x){ 
     background.position = ccp(background.position.x - 30*dt,background.contentSize.height/2); 
     background2.position = ccp(background.position.x+background.contentSize.width,background2.contentSize.height/2); 
    }else{ 
     background2.position = ccp(background2.position.x- 30*dt,background2.contentSize.height/2); 
     background.position = ccp(background2.position.x+background2.contentSize.width ,background.contentSize.height/2); 

    } 

    //reset offscreen position 
    if (background.position.x <-background.contentSize.width/2) 
    { 
     background.position = ccp(background2.position.x+background2.contentSize.width,background.contentSize.width/2); 
    }else if (background2.position.x < -background2.contentSize.width/2) 
    { 
     background2.position = ccp(background.position.x+background.contentSize.width, background2.contentSize.width/2); 
    } 
}