您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x——CCCallFunc家族例子

2013-10-31 11:31 274 查看
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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: