[Cocos2d-x] Day 4 - BackGround & CameraScroll & File Input And Output
| cocos2D/2D Project 2016. 6. 9. 21:53<DAY> 2016.05.21 |
|||||||||
< WORK LIST > | |||||||||
<Class> Object_Hero.cpp & Box_Manager.cpp & Object_BackGround.cpp | |||||||||
- RayCast Revise |
|||||||||
- File Input And Output |
|||||||||
- BackGround Output & Camera Scroll |
1 |
RayCast Revise |
만들어진 Box중에 제일 작은 Box의 크기를 저장시켜 그 박스의 크기만큼 RayCast에서 이동시킨다.
1 2 3 4 5 | Object_Box * Box_Manager::AddBox( CGRect rc ) { if( m_fMinSize > rc.size.width ) m_fMinSize = rc.size.width - 1; if( m_fMinSize > rc.size.height ) m_fMinSize = rc.size.height - 1; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // Box_Manager.cpp bool Box_Manager::Raycast( CGPoint _start, CGPoint _vec, RaycastHit *_hit, float _Distance ) { for( _hit->fDistance = 0; _hit->fDistance < _Distance; _hit->fDistance += m_fMinSize ) { _start.x += _vec.x; _start.y += _vec.y; Object_Box * p = CollisionCheck( _start ); if( p == NULL ) continue; _hit->ObTarget = p; return true; } return false; } | cs |
이렇게 한 이유는 플레이어가 어떠한 크기의 Box와 충돌을 하여도 통과하지 않고 충돌을 일으키게 하기 위함이다.
2 |
File Input And Output |
Save
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | void Box_Manager::SaveBox() { int Count = 0; FILE * pFile = fopen( "BoxData.txt", "wb" ); if( pFile == NULL ) return; for( int i = 0; i < MAXBOX; i++) { if( m_pBox[i] == NULL ) continue; if( m_pBox[i]->getActive() == false ) continue; Count++; } fwrite( &Count, sizeof( int ), 1, pFile ); for( int i = 0; i < MAXBOX; i++) { if( m_pBox[i] == NULL ) continue; if( m_pBox[i]->getActive() == false ) continue; fwrite( &m_pBox[i]->rc, sizeof( CGRect ), 1, pFile ); } fclose( pFile ); } | cs |
Load
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // Box_Manager.cpp void Box_Manager::LoadBox( SceneTower * Scene ) { int Count = 0; FILE *pFile = fopen( "BoxData.txt", "rb" ); if( pFile == NULL ) return; fread( &Count, sizeof( int ), 1, pFile ); for( int i = 0; i < Count; i++) { fread( &m_Rrc, sizeof( CGRect ), 1, pFile ); Scene->addChild( AddBox( m_Rrc ) ); } fclose( pFile ); } | cs |
Box의 x,y좌표와 Width, Height크기만 저장하고, 게임을 켤때 좌표와 크기를 불러오면서
다시 AddBox해준다.
3 |
BackGround Output & Camera Scroll |
배경 출력을 위한 class 및 함수 생성.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // Object_BackGround.cpp #define MAXBG 4 bool Object_BackGround::init() { if( !CCSprite::init() ) return false; char szFileName[256] = {0}; CCSprite *pBg = new CCSprite; for( int i = 0; i < MAXBG; i++ ) { sprintf_s( szFileName, "Resource/BackGround/BG_%d.png", i ); pBg = CCSprite::spriteWithFile( szFileName ); pBg->setScale( 2 ); pBg->setAnchorPoint( ccp( 0, 0.5f ) ); if( i == 1 ) pBg->setPosition( ccp(3915 * i, 2 ) ); else pBg->setPosition( ccp(3915 * i, 0 ) ); this->addChild( pBg ); } schedule( schedule_selector( Object_BackGround::update )); return true; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // Scene_Tower.cpp void SceneTower::BackGroundScroll(float dt) { CGPoint HeroPos = m_pHero->getPosition(); CGPoint MapPos = this->getPosition(); float fWorldPosX = HeroPos.x + MapPos.x; float fWorldPosY = HeroPos.y + MapPos.y; float fMaxPosX = m_Bga->getContentSize().width - CCDirector::sharedDirector()->getWinSize().width; float fMaxPosY = m_Bga->getContentSize().height - CCDirector::sharedDirector()->getWinSize().height; if( fWorldPosX > MAXWIDTH ) MapPos.x -= 500.f * dt; if( fWorldPosX < MINWIDTH ) MapPos.x += 500.f * dt; if( MapPos.x > 0 ) MapPos.x = 0; // if( MapPos.x < -fMaxPosY ) MapPos.x = -fMaxPosY; if( fWorldPosY > MAXHEIGHT ) MapPos.y -= 500.f * dt; if( fWorldPosY < MINHEIGHT ) MapPos.y += 500.f * dt; // if( MapPos.y > 0 ) MapPos.y = 0; // if( MapPos.y < -fMaxPosY ) MapPos.y = -fMaxPosY; this->setPosition( MapPos ); } | cs |
평 가 |
.난이도 : 보통 별 점 : ★★★☆☆ 한 마디: 파일 입출력을 오랜만에 썼 기 때문에 조금 애 먹었다. |
<Progress List> |
- Key Control |
- Box CollisionCheck |
- New File Input And Output - <Complete List> |
- New BackGround Output - <Complete List> |
- New Camera Scroll |
<Complete List> |
- Gravity Jump |
- MouseDrag Box Create |
- New RayCast |
- New File Input And Output |
- New BackGround Output |
'cocos2D > 2D Project' 카테고리의 다른 글
[Cocos2d-x] Day 6 - Resource Work & Bullet Class, Manager Create (0) | 2016.06.12 |
---|---|
[Cocos2d-x] Day 5 - Scroll Map Box & Player Resource Work (0) | 2016.06.12 |
[Cocos2d-x] Day 3 - MouseDrag Box Create & RayCast Collision (0) | 2016.06.08 |
[Cocos2d-x] Day 2 - Create Box / Box CollisionCheck (0) | 2016.06.08 |
[Cocos2d-x] Day 1 - KeyInput & Gravity Jump (0) | 2016.06.08 |