티스토리 뷰

CC_CALLBACK_N 함수

cocos2d-x에서 주로 사용하는 콜백함수 

종류별로 CC_CALLBACK_0, CC_CALLBACK_1, CC_CALLBACK_2 등 뒤에 숫자가 붙는다. 

기본적으로 뒤에 붙는 숫자는 설정한 콜백함수에 자동으로 전달되는 매개변수의 수를 의미한다.

필요한 매개변수는 숫자에 상관없이 전달시킬 수 있다.


CC_CALLBACK_0

CallFunc와 함께 사용된다.

실행하는 주체를 전달하지 못한다.


bool HelloWorld::init(){

    if (!Layer::init()){

        return false;

    }

    

    auto spr = Sprite::create("CloseNormal.png");

    auto act1 = MoveTo::create(3, Point(100, 100));

    auto callback = CCCallFunc::create(CC_CALLBACK_0(HelloWorld::playSound, this));

    auto seq = Sequence::create(act1, callback, NULL);

    

    this->addChild(spr);

    spr->runAction(seq);

    

    return true;

}


void HelloWorld::playSound(){

    CCLOG("play sound");

}



CC_CALLBACK_1
CallFuncN과 함께 사용된다.
실행하는 주체를 전달한다.


bool HelloWorld::init(){

    if (!Layer::init()){

        return false;

    }

    

    auto spr = Sprite::create("CloseNormal.png");

    auto act1 = MoveTo::create(3, Point(100, 100));

    auto callback = CCCallFuncN::create(CC_CALLBACK_1(HelloWorld::playSound, this));

    auto seq = Sequence::create(act1, callback, NULL);

    

    this->addChild(spr);

    spr->runAction(seq);

    

    return true;

}


void HelloWorld::playSound(Ref *sender){   

    CCLOG("play sound");

}


CC_CALLBACK_2

터치 이벤트를 구현할 때 사용한다.

주체와 이벤트를 자동으로 전달한다.


bool HelloWorld::init(){

    if (!Layer::init()){

        return false;

    }

    

    auto listener = EventListenerTouchOneByOne::create();

    listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

    

    return true;

}


bool HelloWorld::onTouchBegan(Touch *touch, Event *event){

    CCLOG("click");

    return true;

}



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함