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