만들면서배우는Cocos2d-x(12-13)2.
화면을 터치한 채로 좌우로 드래그하면 헬
리콥터가 따라서 이동
1.
2.
3.
4.
터치 시작좌표 저장
(이동시) 이동거리 = 현재좌표 – 이전좌표
2의 값만큼 헬리콥터 이동
현재좌표 저장
조작속도 설정
이동거리 *= 배율
4.
GameLayer.cpp
bool GameLayer::init()
{
...
m_moveRate = 2.0f;
setTouchEnabled(true);
return true;
}
// 레이어 터치기능 활성화
void GameLayer::ccTouchesBegan(CCSet* pTouches, CCEvent* pEvent)
{
// 터치 시작좌표 저장
CCTouch* pTouch = (CCTouch*)pTouches->anyObject();
m_prevTouchPos = pTouch->getLocation();
}
5. void GameLayer::ccTouchesMoved(CCSet* pTouches, CCEvent* pEvent)
{
if( m_pHeli != NULL ) {
CCTouch* pTouch = (CCTouch*)pTouches->anyObject();
// 이동거리= 현재좌표– 이전좌표
float diff = pTouch->getLocation().x - m_prevTouchPos.x;
// 이동거리*= 배율
diff *= m_moveRate;
// 헬리콥터이동
CCPoint heliPos = ccpAdd(m_pHeli->getPosition(), ccp(diff, 0.0f));
if( heliPos.x < 0.0f ) heliPos.x = 0.0f;
if( heliPos.x > m_winSize.width ) heliPos.x = m_winSize.width;
m_pHeli->setPosition(heliPos);
// 현재좌표저장
m_prevTouchPos = pTouch->getLocatoin();
}
}
8.
Enemy.h
class Enemy : public CCSprite
{
Enemy(CCSpriteBatchNode* pBatchNode, int zOrder);
~Enemy();
static Enemy* create(CCSpriteBatchNode* pBatchNode, int zOrder);
CCSpriteBatchNode* m_pBatchNode;
}
9.
Enemy.cpp
Enemy::Enemy(CCSpriteBatchNode* pBatchNode, int zOrder)
{
m_pBatchNode = pBatchNode;
}
Enemy:: ~Enemy() {}
Enemy* Enemy:: create(CCSpriteBatchNode* pBatchNode, int zOrder)
{
// 이미지 로드
CCSpriteFrame* pFrame =
CCSpriteFrameCache::sharedSpriteFrameCache()->
spriteFrameByName(“Enemy.png”);
10. // enemy object 생성
Enemy* pEnemy = new Enemy(pBatchNode, zOrder);
if( pEnemy && pEnemy->initWithSpriteFrame(pFrame) ) {
pBatchNode->addChlid(pEnemy, zOrder);
pEnemy->autorelease();
return pEnemy;
}
CC_SAFE_DELETE(pEnemy);
return NULL;
}
11.
적 비행기 여러 대를 가로로 나란히 배치
화면 너비 : 768, 비행기 가로크기 : 150
=> X좌표 : 3.0f + 가로*0.5f + i*(150.0f + 3.0f)
GameLayer.h
std::vector<Enemy*> m_enemies;
GameLayer::Init()
for( int i=0; i < NUM_OF_ENEMIES; ++i ) {
Enemy* pEnemy = Enemy::create(m_pBatchNode, zEnemy);
CCSize contentSize = pEnemy->getContentSize();
pEnemy->setPosition(ccp(3.0f + contentSize.width*0.5f +
i*(150.0f+3.0f),
m_winSize.height – contentSize.height*0.5f));
m_enemies.push_back(pEnemy);
}
12.
적 비행기가 화면 아래로 사라지면 즉시 위에서 다시 등장
bool GameLayer::init()
{
pEnemy->setVisible(false);
}
void GameLayer::update(dt)
{
MoveBackground(dt);
SpawnEnemies();
}
// 적 비행기 생성 for루프에 추가
13. void GameLayer::SpawnEnemies()
{
// 모든 이동이 끝날 때까지 대기
for( int i=0; i < m_enemies.size(); ++i )
{
Enemy* pEnemy = m_enemies[i];
if( pEnemy->isVisible() ) return
}
for( int i=0; i < m_enemies.size(); ++i )
{
Enemy* pEnemy = m_enemies[i];
CCSize contentSize = pEnemy->getContentSize();
// 초기화
pEnemy->stopAllActions();
pEnemy->setVisible(true);
pEnemy->setPosition(CALC_ENEMY_POS(i, contentSize));
14. // 액션(이동) 설정
float distance = m_winSize.height + contentSize.height;
float duration = distance / 200.0f;
CCCallFuncN* pCallFuncN =
CCCallFuncN::create(this, callfuncN_selector(GameLayer::OnEndOfMoving)
);
CCMoveBy* pMoveBy = CCMoveBy::create(duration, ccp(0.0f, -distance));
pEnemy->runAction(CCSequence::create(pMoveBy, pCallFuncN, NULL));
}
}
void GameLayer::OnEndOfMoving(CCNode* pNode)
{
// 이동이 끝난 비행기는 숨김
pNode->setVisible(false);
}
15.
적 비행기가 소멸되면 일정시간 후 재등장
bool GameLayer::init()
m_enemySpawnTime = Common::GetCurrentTimeInMilli();
void GameLayer::update(dt)
if( m_enemySpawnTime != 0 && m_enemySpawnTime +
ENEMY_SPAWN_COOLTIME <= Common::GetCurrentTimeInMilli() )
SpawnEnemies();
void GameLayer::SpawnEnemies()
m_enemySpawnTime = 0;
void GameLayer::OnEndOfMoving(CCNode* pNode)
m_enemySpawnTime = Common::GetCurrentTimeInMilli();
16.
V자 대형
가운데를 기준으로 바깥쪽으로 갈수록 일정 크기만큼씩 올라감.
for( int i=0; i < m_enemies.size(); ++i )
{
...
float diffY = abs(i - 2) * contentSize.height * 0.5f;
contentSize.height += diffY;
...
}
17. void CCSprite::setColor(ccColor3B color);
static ccColor3B changeColors[NUM_OF_CHANGE_COLOR] = {
ccc3(255, 255, 127);
ccc3(127, 255, 255);
ccc3(255, 127, 255);
};
void Enemy::Update(float dt)
{
if( m_colorChangeTime + CHANGE_COLOR_COOLTIME <= currentTime ) {
m_colorChangeTime = currentTime;
m_colorChangeIndex = ++m_colorChangeIndex % NUM_OF_CHANGE_COLOR;
setColor(changeColors[m_colorChangeIndex]);
}
}