2011-01-02 2 views
0

무한 배경 스크롤이 완료되면 오브젝트가 고정되어 있습니다 (예 : 낙서 점프, Papi 점프의 Papy) 또는이 오브젝트가 실제로 움직입니다. 배경 이동 만합니다. 또는 두 가지 (배경과 개체) move.plz 누군가 나를 도울 수 있습니다. 나는이 솔루션을 4/5 일 동안 찾고 있지만 솔루션을 얻을 수 없습니다. 그래서 누군가가 나를 도와줍니다. 그리고 만약 물체가 물체가 움직이는 환상을 만드는 방법을 움직이지 않는다면.cocos2d에서 배경을 스크롤 할 때 객체가 고정되어 있습니까?

답변

1

개체를 스크롤하는 배경과 같은 레이어에 추가하면 배경 스크롤과 함께 스크롤됩니다.

당신이 낙서 점프에서 영웅과 같은 효과를 찾고 있다면 장면에 두 개 이상의 레이어가있는 것을 볼 수 있습니다.

  1. 계층 1 : 스크롤 배경 레이어
  2. 레이어 2 : 스프라이트 레이어

SomeScene.m

CCLayer *backgroundLayer = [[CCLayer alloc] init]; 
CCLayer *spriteLayer= [[CCLayer alloc] init]; 

[self addChild:backgroundLayer z:0]; 
[self addChild:spriteLayer z:1]; 

//Hero stays in one spot regardless of background scrolling. 
CCSprite *squidHero = [[CCSprite alloc] initWithFile:@"squid.png"]; 
[spriteLayer addChild:squidHero]; 

당신이 객체가 배경에 추가 배경으로 스크롤하려면 층 :

//Platform moves with background. 
CCSprite *bouncePlatform= [[CCSprite alloc] initWithFile:@"bouncePlatform.png"]; 
[backgroundLayer addChild:bouncePlatform]; 
+0

좋아, - 고마워 내가 어떻게 같은 time.So에서이 2 개의 레이어를 실행하는 낙서와 platform.which 사이의 충돌을 알아 내기 위해 다른 이시군요을하는이이 2 개의 다른 layer.Because를 동기화하는 방법 지금까지 단 하나의 레이어 만 사용 했으므로 2 개의 레이어를 함께 실행하고 충돌을 확인하는 방법. – russell

0

또 다른 대안은 CCFollow 작업을 사용하는 것입니다. 배경이 정적 인 것처럼 코딩하고 플레이어가 움직이는 것처럼 코딩하고 플레이어에 CCFollow 액션을 추가합니다. 이것은 기본적으로 카메라를 움직여 플레이어를 추적합니다.

오프셋을 따르도록 (즉, 플레이어가 화면 중앙에 있지 않도록) CCFollow 동작을 수행 할 수 있도록 클래스를 수정하고 스무딩 효과를 줄 수도 있습니다. 플레이어가 움직이면 팔로우 액션이 번지지 않게됩니다. 아래 코드를 참조하십시오 :

* 참고 cocos2d-x, C++ 포트를 사용하고 있습니다. 이 메소드는 cocos2d에서 유사하며 cocos2d 구문에 맞게이 메소드를 수정할 수 있어야합니다. 또는 주위를 검색 - 내가 cocos2d에 대한 이들을 발견하고 C로 포팅 + +.

//defines the action to constantly follow the player (in my case, "runner.p_sprite is the sprite pointing to the player) 
FollowWithOffset* followAction = FollowWithOffset::create(runner.p_sprite, CCRectZero); 
runAction(followAction); 

그리고 CCFollow 내 자신의 클래스, CCFollowWithAction을 만들 별도로, 나는 클래스 정의를 복사 한. 이것은 또한 플레이어가 움직일 때, 동작이 덜컹 거리지 않도록 부드럽게하는 효과 (더 온라인으로 볼 수 있습니다)가 있습니다. "initWithTarget"을 수정하여 오프셋을 고려하고 "단계"를 사용하여 스무딩 작업을 추가했습니다. 아래 주석에서 수정 내용을 볼 수 있습니다.

bool FollowWithOffset::initWithTarget(CCNode *pFollowedNode, const CCRect& rect/* = CCRectZero*/) 
{ 
    CCAssert(pFollowedNode != NULL, ""); 

    pFollowedNode->retain(); 
    m_pobFollowedNode = pFollowedNode; 
    if (rect.equals(CCRectZero)) 
    { 
     m_bBoundarySet = false; 
    } 
    else 
    { 
     m_bBoundarySet = true; 
    } 

    m_bBoundaryFullyCovered = false; 

    CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 
    m_obFullScreenSize = CCPointMake(winSize.width, winSize.height); 

    //m_obHalfScreenSize = ccpMult(m_obFullScreenSize, 0.5f); 
    m_obHalfScreenSize = CCPointMake(m_obFullScreenSize.x/2 + RUNNER_FOLLOW_OFFSET_X, 
            m_obFullScreenSize.y/2 + RUNNER_FOLLOW_OFFSET_Y); 

    if (m_bBoundarySet) 
    { 
     m_fLeftBoundary = -((rect.origin.x+rect.size.width) - m_obFullScreenSize.x); 
     m_fRightBoundary = -rect.origin.x ; 
     m_fTopBoundary = -rect.origin.y; 
     m_fBottomBoundary = -((rect.origin.y+rect.size.height) - m_obFullScreenSize.y); 

     if(m_fRightBoundary < m_fLeftBoundary) 
     { 
      // screen width is larger than world's boundary width 
      //set both in the middle of the world 
      m_fRightBoundary = m_fLeftBoundary = (m_fLeftBoundary + m_fRightBoundary)/2; 
     } 
     if(m_fTopBoundary < m_fBottomBoundary) 
     { 
      // screen width is larger than world's boundary width 
      //set both in the middle of the world 
      m_fTopBoundary = m_fBottomBoundary = (m_fTopBoundary + m_fBottomBoundary)/2; 
     } 

     if((m_fTopBoundary == m_fBottomBoundary) && (m_fLeftBoundary == m_fRightBoundary)) 
     { 
      m_bBoundaryFullyCovered = true; 
     } 
    } 

    return true; 
} 

void FollowWithOffset::step(float dt) 
{ 
    CC_UNUSED_PARAM(dt); 

    if(m_bBoundarySet){ 
     // whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased 
     if(m_bBoundaryFullyCovered) 
      return; 

     CCPoint tempPos = ccpSub(m_obHalfScreenSize, m_pobFollowedNode->getPosition()); 

     m_pTarget->setPosition(ccp(clampf(tempPos.x, m_fLeftBoundary, m_fRightBoundary), 
            clampf(tempPos.y, m_fBottomBoundary, m_fTopBoundary))); 
    } 
    else{ 
     //custom written code to add in support for a smooth ccfollow action 
     CCPoint tempPos = ccpSub(m_obHalfScreenSize, m_pobFollowedNode->getPosition()); 
     CCPoint moveVect = ccpMult(ccpSub(tempPos,m_pTarget->getPosition()),0.25); //0.25 is the smooth constant. 
     CCPoint newPos = ccpAdd(m_pTarget->getPosition(), moveVect); 
     m_pTarget->setPosition(newPos); 
    } 
} 
관련 문제