cocos2d-x 공부용2014. 9. 15. 11:36

원글 주소: 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();
}

실행 결과 화면


 

터치하는 동안은 흐릿하게 되고, 손을 떼면 사라진다.

------------------------------------------------------------------------------------------------

특별히 설명이 필요한 부분이 아니면 그냥 소스만 넣고 있는데 설명이 필요할지 모르겠습니다.

보고 계신 분이 있으신지 모르겠고..

원래 목적대로 소스 변경만 하고 특별히 다른 걸 추가하진 않고 진행합니다.



Posted by 아이시네프