cocos2d-x 공부용2014. 9. 17. 11:44

원글 출처 : http://horns.tistory.com/17


-12- 점수 계산


1. 점수 이미지&폰트

원글 출처에서 이미지랑 fnt 파일을 받아 Resources 폴더에 넣어주시기 바랍니다.


2.  CCLabelBMFont


HelloWorldScene.h

class HelloWorld : public cocos2d::Layer
{
private:
...
	int _score;
	LabelBMFont* _scoreFont;

public:
...
    void addGameScore(int deadCoinNum);

};


HelloWorldScene.cpp

void HelloWorld::createGameScene() {
...
	//점수 이미지 추가
	_scoreFont = LabelBMFont::create("0", "font.fnt", _screenSize.width*0.3f);
	_scoreFont->setAnchorPoint(ccp(1, 0.5));
	_scoreFont->setPosition(ccp(_screenSize.width*0.5f, _screenSize.height*0.95f));
	_scoreFont->setScale(1.5f);
	this->addChild(_scoreFont);

	initGameCoin();
}

void HelloWorld::initGameCoin() {
	int coinX = 0;
	int coinY = 0;
	int diffX = _screenSize.width * 0.135f; //코인 x 간격
	int diffY = _screenSize.height * 0.097f; //코인 y 간격
	int initCoinX = _screenSize.width * 0.095f; //초기 코인 x 위치
	int initCoinY = _screenSize.height * 0.613f; //초기 코인 y 위치

	GameCoin* gameCoin;
	_lastCoin = -1;
	_selectCoinCount = 0;
	_score = 0;
...
}

void HelloWorld::addGameScore(int deadCoinNum){
	char score[100] = {0,};

	_score += (deadCoinNum*100); //한코인당 100점

	sprintf(score, "%i", _score);
	_scoreFont->setString(score);
}

void HelloWorld::clearSelectCoin() {
	int index;
	GameCoin* tmpCoin;

	if (_selectCoinCount >= 3) {
		addGameScore(_selectCoinCount);

		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();
}

실행 화면 :



 




설명 생략합니다.

Posted by 아이시네프