5.触摸
在初始化函数中: setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne 在.h中声明: boolccTouchBegan(CCTouch*pTouch,CCEvent*pEvent);在.cpp中实现: LayerGame::ccTouchBegan(*pTouch,255); font-size:9.5pt">pEvent) { staticinttouchOrder=1;for(i=0;i<_rank*_rank;i++){//注意boundingBox()前面被括号括起来了,是个整体if(((CardItem*)allCards->objectAtIndex(i))->boundingBox().containsPoint(pTouch->getLocation())){//只有当前的触摸次序与数字卡的序号相等,才能继续下去(touchOrder==((*)allCards->objectAtIndex(i))->getIdx())touchOrder++;((*)allCards->objectAtIndex(i))->removeFromParentAndCleanup(true);}(touchOrder==_rank*_rank+1)CCLog("youWin"touchOrder=1;//重置触摸次序CCScene*scene=GameOver::scene(_rank,passtime);CCDirector::sharedDirector()->replaceScene(scene);returnfalse;
|
6.结束场景:
结束场景由 2.返回主场景的Redo菜单 3.返回开始场景的Back菜单 组成
结束场景接收2个参数: 1.难度等级rank 2.完成时间passtime
Passtime不再它用 Rank还要返回,在.h中设置为全局 int_rank; |
|
|
参数传递与保存: |
||
class:publicCCLayer :*scene(rank,floatt);//CREATE_FUNC(GameOver)*create(boolinit(voidredoCallBack(CCObject*);backCallBack(*); |
CCScene*GameOver::scene(rankfloatt{。。。。。。returnscene;} ::create(*pRet=new;pRet;::init(_rank= |
|
在.h中 voidredoCallBack(CCObject*);backCallBack(*); |
在.cpp中 CCMenuItem*redo=CCMenuItemFont::create("Redo");redo->setTarget(this,menu_selector(::redoCallBack));CCMenuItem*back="Back");back->setTarget(::backCallBack));CCMenu*menu=::create(redo,back,255); font-size:9pt">NULLmenu->alignItemsHorizontallyWithPadding(30);addChild(menu); |
|
::redoCallBack(*)CCDirector::sharedDirector()->replaceScene(::scene(_rank));::backCallBack(GameStart::scene());} |
7.定时器与计时
主场景,在.h中 mySchedule(delta);passtime; |
|
在.cpp中,调用和实现 |
|
passtime=0.0; schedule(schedule_selector( ayerGame::mySchedule),0.1); CCLabelTTF*ttf=::create("0.00","Arial" addChild(ttf,1313);ttf->setPosition(ccp(winSize.width/2+50,255); font-size:8pt">.height-50));
注意:ttf的Tag |
voidLayerGame::mySchedule(floatdelta){passtime+=; CCString*str= ::createWithFormat("%0.2f"*ttf=(*)getChildByTag(1313);ttf->setString(str->getCString()); |
中文显示在cocos2d-x中不支持,要想显示中文,其中一种方法是使用CCDictionary。即自己写一个.xml文件,Value是中文,再通过Key拿中文
|
CCDictionary*dic=::createWithContentsOfFile("chinese.xml"CCString*str1=(*)dic->objectForKey("simple"*str2=("normal"*str3=("hard"*str4=("quit"); |
*simple=CCMenuItemFont::create(str1->getCString());simple->setColor(ccGREEN); *normal=::create(str2->getCString()); ....... |
声音要使用到cocosdention库
|
在VS中这个是没什么好说的
但在Linux中会链接出错:
必须在Makefile文件中-lcocosdenshion SHAREDLIBS+=-lcocos2d-lcocosdenshion |
在init()中预加载: CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("click.wav" CCTouchBegan()中:如果点击正确的数字,发出声音:if(touchOrder==((CardItem*)allCards->objectAtIndex(i))->getIdx()) 。。。::sharedEngine()->playEffect(。。。} |