cocos2d0基础篇笔记二

1.菜单的使用:

CCMenuItemimage*image=CCMenuItemImage*create("xxx.png",

"xxx,png",

"xxx.png",

this,

menu_selector(HelloWorld::selfdefinefunc));//第一个參数:未选中时使用的图片。第二个參数:选中时使用的图片,第三个參数:进入时选用的图片。第四个參数:在哪个对象上,第四个參数:详细的响应函数。

CCMenu*menu=CCMenu::create(iamge,NULL);//创建菜单

menu->addChild(menu);


2.精灵运行的动作:

CCMoveBy*moveby=CCMoveBy::create(2.0f,ccp(800,0));

CCCallFuncN*selfaction=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::selfdefine));//CCCallFuncN也是一个动作,仅仅只是这个动作是回调一个函数(带一个參数)。
CCSequence*action=CCSequence::create(moveby,selfaction,NULL);//组合动作
sprite1->runAction(action);


3.如何开启定时器:

bool HelloWorld::init()
{

//省略非常多代码

this->schedule(schedule_selector(HelloWorld::usecreatesprite),2);//第一个參数:响应函数。第二个參数:间隔多久运行一次。

return true;

}

void HelloWorld::usecreatesprite(float dt){
createsprite();
}


void HelloWorld::createsprite(){
CCSize visiblesize=CCDirector::sharedDirector()->getVisibleSize();
CCSprite* sprite1=CCSprite::create("sprite.png");//由于要创建非常多精灵,所以使用局部变量,不适用类成员。


int y=rand()%(int)(visiblesize.height);
sprite1->setPosition(ccp(10,y));
 this->addChild(sprite1);
CCMoveBy*moveby=CCMoveBy::create(2.0f,ccp(800,0));

CCCallFuncN*selfaction=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::selfdefine));
CCSequence*action=CCSequence::create(moveby,selfaction,NULL);
sprite1->runAction(action);
}

提问:为什么在用定时器时不直接启用createsprie函数,而是在usecreatesprite(float dt)函数中调用?

        由于schedule里的运行函数的原型是固定的,返回值是void型。參数类型是float。而createsprite函数不符合要求,所以用usecreatesprite函数起到一个过渡函数的作用。

posted @ 2017-05-15 20:49  llguanli  阅读(164)  评论(0编辑  收藏  举报