원글 출처 : http://horns.tistory.com/15
-11- 새로운 코인을 생성
1. 사라진 코인의 개수는?
HelloWorldScene.h
class HelloWorld : public cocos2d::Layer { private: .. int _lineDeadCoin[BOARD_X]; public: ... void addNewCoin(); void setNewCoin(GameCoin* coin); void addNewCoinAction(); };
2. 코인 생성
HelloWorldScene.cpp
void HelloWorld::addNewCoin() { GameCoin* coin; int line = 0; for (int i = 0; i < BOARD_X; i++) { _lineDeadCoin[i] = 0; } for (int i = 0; i < _gameCoins->count(); i++) { coin = (GameCoin*) _gameCoins->objectAtIndex(i); if (coin->getState() == GameCoin::DEAD) { line = (int) (i / BOARD_Y); _lineDeadCoin[line] += 1; setNewCoin(coin); } } addNewCoinAction(); }
void HelloWorld::setNewCoin(GameCoin* coin){ String* name; Texture2D* texture; int coinType = rand() % TOTAL_COIN_TYPE + 1; //반드시 +는 띄어쓰기 해야함. 아니면 rand()%4가 아니라 rand()%5가 될 수 있음. name = String::createWithFormat("coin_0%i.png", coinType); texture = TextureCache::sharedTextureCache()->addImage(name->getCString()); coin->setTexture(texture); coin->setVisible(true); coin->setType(coinType); coin->setState(GameCoin::LIVE); }
void HelloWorld::addNewCoinAction(){ GameCoin* coin; Point pos; int startIndex; int diffY = _screenSize.height * 0.097f; for(int i = 0; i < BOARD_X; i++){ if(_lineDeadCoin[i] > 0){ startIndex = i*BOARD_Y; for(int j = startIndex; j< startIndex+_lineDeadCoin[i]; j++){ coin = (GameCoin*)_gameCoins->objectAtIndex(j); pos = coin->getPosition(); coin->setPosition(ccp(pos.x, pos.y+(_lineDeadCoin[i]*diffY))); moveCoin(coin, pos); } } } }
void HelloWorld::onTouchEnded(Touch* touch, Event* event) { auto target = event->getCurrentTarget(); Point location = target->convertToNodeSpace(touch->getLocation()); clearSelectCoin(); moveUpDeadCoin(); addNewCoin(); }
실행 결과
새로운 코인이 위에서 내려옵니다.
원글 출처 : http://horns.tistory.com/14
-10- 코인 애니메이션
1. Move State
GameCoin.h
class GameCoin : public Sprite { private: public: enum gameState{ LIVE, DEAD, SELECT, MOVE, };
2. 움직이는 액션 추가
HelloWorldScene.h
class HelloWorld : public cocos2d::Layer { private: .. public: .. void moveCoin(GameCoin* coin, Point& pos); void coinMoveDone(Node* pSender); };
HelloWorldScene.cpp
void HelloWorld::moveCoin(GameCoin* coin, Point& pos){//추가 float duration = 0.0f; float coinSpeed = 1000.0f; Point prevPos = coin->getPosition(); coin->setState(GameCoin::MOVE); coin->stopAllActions(); duration = prevPos.getDistance(pos)/coinSpeed; FiniteTimeAction* movePos = Sequence::create( MoveTo::create(duration, pos), CallFuncN::create(this, callfuncN_selector(HelloWorld::coinMoveDone)), NULL); coin->runAction(movePos); }
void HelloWorld::coinMoveDone(Node* pSender){//추가 GameCoin* coin = (GameCoin*)pSender; coin->setState(GameCoin::LIVE); }
void HelloWorld::changeCoin(int index1, int index2){ Point tmpPos; GameCoin* tmpCoin1 = (GameCoin*)_gameCoins->objectAtIndex(index1); GameCoin* tmpCoin2 = (GameCoin*)_gameCoins->objectAtIndex(index2); tmpPos = tmpCoin2->getPosition(); tmpCoin2->setPosition(tmpCoin1->getPosition()); // tmpCoin1->setPosition(tmpPos); moveCoin(tmpCoin1, tmpPos); _gameCoins->exchangeObjectAtIndex(index1, index2); }
실행 결과 코인이 아래로 움직이면서 내려오면 성공입니다.
원글 출처 : http://horns.tistory.com/12
코인을 없애고 어떻게 위의 코인을 아래로 내려 배치할 것인가에 대한 구상입니다.
여기선 생략합니다.
원글 출처 : http://horns.tistory.com/13
-9- 코인을 사라지게 하자
1. 사라질 코인을 위로 보내기
HelloWorldScene.h
class HelloWorld : public cocos2d::Layer { ... public: ... void moveUpDeadCoin(); void changeCoin(int index1, int index2); };
HelloWorldScene.cpp
void HelloWorld::onTouchEnded(Touch* touch, Event* event) { auto target = event->getCurrentTarget(); Point location = target->convertToNodeSpace(touch->getLocation()); clearSelectCoin(); moveUpDeadCoin(); }
void HelloWorld::moveUpDeadCoin(){ int deadCoinNum = 0; int bottomPos = 0; GameCoin* coin; for(int x=0; x< BOARD_X; x++){ bottomPos=((x+1)*BOARD_Y)-1; deadCoinNum = 0; for(int y=bottomPos; y>bottomPos-BOARD_Y; y--){ coin = (GameCoin*)_gameCoins->objectAtIndex(y); if(coin->getState() == GameCoin::DEAD){ deadCoinNum++; continue; } if(deadCoinNum>0){ changeCoin(y,y+deadCoinNum); } } } }
void HelloWorld::changeCoin(int index1, int index2){ Point tmpPos; GameCoin* tmpCoin1 = (GameCoin*)_gameCoins->objectAtIndex(index1); GameCoin* tmpCoin2 = (GameCoin*)_gameCoins->objectAtIndex(index2); tmpPos = tmpCoin2->getPosition(); tmpCoin2->setPosition(tmpCoin1->getPosition()); tmpCoin1->setPosition(tmpPos); _gameCoins->exchangeObjectAtIndex(index1, index2); }
실행 결과
없앤만큼 내려옵니다.
원글 주소: http://horns.tistory.com/10
-7- 터치한 코인 제거하기
1. 선택한 코인 개수를 저장
HelloWorldScene.h
class HelloWorld : public cocos2d::Layer { private: enum{ BOARD_X = 7, //코인 x 개수 ... int _selectCoinCount; public: ... void clearSelectCoin(); void resetSelectMask(); void resetGameInfo(); };
HelloWorldScene.cpp
void HelloWorld::initGameCoin() { int coinX = 0; int coinY = 0; ... _selectCoinCount = 0; }
int HelloWorld::addSelectCoins(int index) { if (index < 0) { return -1; } GameCoin* tmpCoin = (GameCoin*) _gameCoins->objectAtIndex(index); Sprite* selectMask = (Sprite*) _selectMask->objectAtIndex(index); if (tmpCoin->getState() != GameCoin::SELECT) { tmpCoin->setState(GameCoin::SELECT); _selectCoins->addObject(tmpCoin); selectMask->setVisible(true); } _selectCoinCount +=1; return 0; }
2. 코인 클리어
HelloWorldScene.cpp
void HelloWorld::onTouchEnded(Touch* touch, Event* event) { auto target = event->getCurrentTarget(); Point location = target->convertToNodeSpace(touch->getLocation()); clearSelectCoin(); }
void HelloWorld::clearSelectCoin() { int index; GameCoin* tmpCoin; if (_selectCoinCount >= 3) { for (index = 0; index < _selectCoins->count(); index++) { tmpCoin = (GameCoin*) _selectCoins->objectAtIndex(index); tmpCoin->setState(GameCoin::DEAD); tmpCoin->setVisible(false); } } else { for (index = 0; index < _selectCoins->count(); index++) { tmpCoin = (GameCoin*) _selectCoins->objectAtIndex(index); tmpCoin->setState(GameCoin::LIVE); } } resetSelectMask(); resetGameInfo(); }
3. 다음 터치를 위한 초기화
HelloWorldScene.cpp
void HelloWorld::resetSelectMask() { Sprite* selectMask; for (int i = 0; i < _selectMask->count(); i++) { selectMask = (Sprite*) _selectMask->objectAtIndex(i); selectMask->setVisible(false); } }
void HelloWorld::resetGameInfo() { _selectCoinCount = 0; _lastCoin = -1; _selectCoins->removeAllObjects(); }
실행 결과 화면
터치하는 동안은 흐릿하게 되고, 손을 떼면 사라진다.
------------------------------------------------------------------------------------------------
특별히 설명이 필요한 부분이 아니면 그냥 소스만 넣고 있는데 설명이 필요할지 모르겠습니다.
보고 계신 분이 있으신지 모르겠고..
원래 목적대로 소스 변경만 하고 특별히 다른 걸 추가하진 않고 진행합니다.