2013-08-20 6 views
2

나는 아직 새로운 게임 인 cocos2dx로 2D 게임을 개발 중입니다 ... 게임 내에서 하나의 그룹으로 묶을 UI 요소가 여러 개 있습니다 (그룹화하려고합니다). CCLayer s). 예를 들어, 몇 가지 텍스트 레이블과 스프라이트는 CCLayerCStatBar을 형성합니다. 이 CStatBar은 다양하게 포함될 것입니다. CCLayerCocos2dx 하위 레이어

어떻게하면됩니까? CStatBar 클래스를 만든 다음 포함하는 클래스의 init() 함수 내에서 I CStatBar::create()을 호출하고 this->addChild(pStatBar)을 호출하면 통계 막대가 나타나지 않습니다 ... 놓친 모든 명백한 문제가 있습니까? 모든 위치가 정확합니다. 감사!

편집 :

주 : 하위 계층의 1 ccTouchesBegan이 라고, 그러나 그것은/렌더링 그것은 단지 부모 레이어의 일부 영역을 커버하도록 2. 어떻게이 하위 계층의 크기를 조정합니까 볼 수 없습니다? 추측 컨대 CStatBar 만 화면이 아닌 전체 화면의 상단 영역의 10 % ... CParent::init() 함수 내에서

답변

3

, 당신은 초기화 할 수 있습니다를 포함한다 CSublayer과 같이 :

 // Create and initialize CSublayer 
    CSublayer* pSublayer= CSublayer::create(); 
    float fWidth = pSublayer->getContentSize().width; 
    float fHeight = pSublayer->getContentSize().height; 
    pSublayer->setPosition(ccp(fWidth/2.0f, fHeight/2.0f)); 
    this->addChild(pSublayer); 

하고 CSublayer 다른 CCLayer처럼 정의 될 수 있습니다. 당신이 CParent 층보다 작아야하는 CSublayer을 제한하려면

, 당신과 같이 자사의 초기화 함수 내에서 수행 할 수 있습니다 :

CSublayer::init() { 
     // initialize the size with the size of the background sprite 
    CCSprite *pSpriteBackground = CCSprite::createWithSpriteFrame(
     CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("background.png") 
    ); 
    this->setContentSize(CCSize(pSpriteBackground->getContentSize().width, pSpriteBackground->getContentSize().height)); 
    pSpriteBackground->setPosition(ccp(fScreenHalfWidth, fScreenHeight-(pSpriteBackground->getContentSize().height/2.0f))); 
    this->addChild(pSpriteBackground); 
}