cocos2d-x 入门游戏:许特尔图_3 of 4

前端之家收集整理的这篇文章主要介绍了cocos2d-x 入门游戏:许特尔图_3 of 4前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


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.结束场景:

结束场景由

1.显示成绩的标签LabelTTF

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

注意:ttfTag

voidLayerGame::mySchedule(floatdelta){passtime+=;

CCString*str=

::createWithFormat("%0.2f"*ttf=(*)getChildByTag(1313);ttf->setString(str->getCString());

8.中文显示与声音

中文显示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(。。。}

原文链接:https://www.f2er.com/cocos2dx/342002.html

猜你在找的Cocos2d-x相关文章