고정된 화면을 이동시켜줄 화면스크롤을 만들생각이다.
일단 함수 이름을 MoveScene으로 지었다.
이 함수는 캐릭터가 기준이 되어 모니터 상에 40%~60%를 넘어가면 맵이 이동하는 기능이다.
메인씬에서 init에서 세팅하고있다.
CGSize size = CCDirector::sharedDirector()->getWinSize();
Left = size.width * 0.4f;
Right = size.width * 0.6f;
Up = size.height * 0.4f;
Down = size.height * 0.6f;
for (int i = 0; i < 4; i++){
pLine[i] = CCSprite::spriteWithFile("dnf\\line.png");
this->addChild( pLine[i] );
pLine[i]->setAnchorPoint( ccp(0.5,0.5) );
pLine[i]->setScaleY(100.f);
}
pLine[0]->setPosition(ccp(Left,0));
pLine[1]->setPosition(ccp(Right,0));
pLine[2]->setPosition(ccp(0, Up));
pLine[3]->setPosition(ccp(0, Down));
pLine[2]->setRotation(90);//업 스프라이트가 서 있어서 눕혀줌
pLine[3]->setRotation(90);//위와 같다.
void MyScene::MoveScene(ccTime dt)
{
CGPoint mapPos = m_pBackGround->getPosition();
float fWorldposX = pHero->getPosition().x + mapPos.x;
float fWorldposY = pHero->getPosition().y + mapPos.y;
if(fWorldposX > Left) m_destPos.x -= 300.f * dt;
if(fWorldposX < Right) m_destPos.x += 300.f * dt;
if(fWorldposY > Up) m_destPos.y -= 300.f * dt;
if(fWorldposY < Down) m_destPos.y += 300.f * dt;
if(m_destPos.x > 0)m_destPos.x = 0;
if(m_destPos.y > 0)m_destPos.y = 0;
float fScreenXEnd = CCDirector::sharedDirector()->getWinSize().width - m_pGround->getContentSize().width;
float fScreenYEnd = CCDirector::sharedDirector()->getWinSize().height - m_pGround->getContentSize().height;
if(m_destPos.x < fScreenXEnd) m_destPos.x = fScreenXEnd;
if(m_destPos.y < fScreenYEnd) m_destPos.y = fScreenYEnd;
CGPoint gap = ccp(m_destPos.x - mapPos.x, m_destPos.y - mapPos.y);
gap.x /= 16;
gap.y /= 16;
mapPos.x += gap.x;
mapPos.y += gap.y;
m_pBackGround->setPosition( mapPos );
}
이해를 돕는 그림
결과 동영상
'Programming > Cocos2D-X' 카테고리의 다른 글
removeChild 맵에 필요없는 블록 삭제 (0) | 2015.04.25 |
---|---|
파일저장 꾸민 맵 저장하기 (0) | 2015.04.24 |
랜더타겟? 메모리 관리? CCSpriteBatchNode!! (0) | 2015.04.24 |
내가 선택한 타일 그리기 그리고 각에 맞춰서 타일 그리기 (0) | 2015.04.23 |
Raycast 만들기 (3) | 2015.04.20 |
CollisionBlock 마우스 클릭으로 생성하기 (0) | 2015.04.20 |
캐릭터 리소스 변경작업 (0) | 2015.04.20 |
CollisionBlock_Manager 만들기 (0) | 2015.04.19 |