cocos2d-x——CCCallFunc家族例子

来源:互联网 发布:删除windows.old后果 编辑:程序博客网 时间:2024/05/16 06:04

boolHelloWorld::init()

{

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

    // 1. super init first

   if ( !CCLayer::init() )

    {

        returnfalse;

    }

    

   CCSprite *sp =CCSprite::create("Icon.png");

    sp->setPosition(ccp(100,100));

   this->addChild(sp,0,1);

    

   CCLabelTTF *ttf =CCLabelTTF::create("函数回调动作","宋体",24);

    CCMenuItemLabel *label =CCMenuItemLabel::create(ttf,this,menu_selector(HelloWorld::menuCallback));

    label->setPosition(ccp(0,80));

   CCMenu *menu =CCMenu::create(label,NULL);

   this->addChild(menu);


    returntrue;

}


voidHelloWorld::menuCallback(CCObject* pSender)

{

   CCSprite* sp = (CCSprite*)this->getChildByTag(1);

   CCActionInterval* move =CCMoveTo::create(1,ccp(300,sp->getPositionY()));

//    //1

    CCCallFunc * funCall =CCCallFunc::create(this,callfunc_selector(HelloWorld::callbackC));

    

    

    

   //2

    //CCCallFuncN * funCall =CCCallFuncN::create(this, callfuncN_selector(HelloWorld::callbackN));

    

    

   //3

//    float *data = (float*)malloc(sizeof(float));

//    *data = 200;

//    CCCallFuncND * funCall =CCCallFuncND::create(this, callfuncND_selector(HelloWorld::callbackND), (void*)data);

    

    

    

//    //4

//    CCSprite *sp1 = CCSprite::create("Icon-72.png");

//    CCCallFuncO * funCall =CCCallFuncO::create(this, callfuncO_selector(HelloWorld::callbackO), (CCObject*)sp1);

//

    

    

    

    

    

    

    

   CCFiniteTimeAction *seq =CCSequence::create(move,funCall,NULL);

    sp->runAction(seq);

    ((CCMenuItemLabel *)pSender)->setEnabled(false);

}


voidHelloWorld::callbackC()

{

   CCSprite* sp = (CCSprite*)this->getChildByTag(1);

   this->removeChild(sp);

   CCLabelTTF *ttf =CCLabelTTF::create("移动动作执行完毕","仿宋",24);

    ttf->setPosition(ccp(300,150));

   this->addChild(ttf);

}


voidHelloWorld::callbackN(CCNode* sender)

{

   this->removeChild(sender);

   CCLabelTTF *ttf =CCLabelTTF::create("N移动动作执行完毕","仿宋",24);

    ttf->setPosition(ccp(300,150));

   this->addChild(ttf);

}


voidHelloWorld::callbackND(CCNode* sender,void* data)

{

   this->removeChild(sender);

   float y = *(float*)data;

   char str[100];

    sprintf(str,"ND移动动作执行完毕,传过来的数据是:%f", y);

   CCLabelTTF *ttf =CCLabelTTF::create(str,"仿宋",24);

    ttf->setPosition(ccp(300, y));

   free(data);

   this->addChild(ttf);

}


voidHelloWorld::callbackO(CCObject* sender)

{

   CCLabelTTF *ttf =CCLabelTTF::create("O移动动作执行完毕","仿宋",24);

    ttf->setPosition(ccp(300,150));

   this->addChild(ttf);

   CCSprite *sp = (CCSprite*)sender;

    sp->setPosition(ccp(300,100));

   this->addChild(sp, -1);

}


原创粉丝点击