본문 바로가기
프로그래밍/cocos2d-x

cocos2d-x sprintf이용해서 점수 표시

by -현's- 2013. 6. 9.
반응형


●sprintf

- 메모리에 출력한다.

- sprintf(문자배열명, "출력문자와 자료형(ex.%d)", 입력 데이터);





●예제

- HelloWorldScene.cpp


#include "HelloWorldScene.h"


using namespace cocos2d;


CCScene* HelloWorld::scene()

{

    CCScene *scene = CCScene::create();

    

    HelloWorld *layer = HelloWorld::create();


    scene->addChild(layer);


    return scene;

}


bool HelloWorld::init()

{

    if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) )

    {

        return false;

    }

////////////////////////



//레이블 생성 및 초기화

CCLabelTTF *pLabel=CCLabelTTF::create("score:0", "Thonburi", 30);

//레이블 위치 지정

pLabel->setPosition(ccp(240,180));

//레이블 색 지정

pLabel->setColor(ccc3(0,0,0));

//레이블의 투명도 지정

pLabel->setOpacity(100.0);

//레이어에 레이블 객체 추가

this->addChild(pLabel);


char coinScore[100] = {0};

int score=0;

for(int i=0; i<100;i++){

score++;

sprintf(coinScore, "score: %d",score);

CCLog("score:%d", score);

pLabel->setString(coinScore);

}



    return true;

}











반응형

댓글