第一、virtual bool init();
元素的创建、是否可触摸一类都在这里面可以先设置。
第二、 static cocos2d::CCScene* scene();
添加层到当前场景
第三、触摸事件
virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
void U3BGameScene::ccTouchesBegan( CCSet *pTouches, CCEvent *pEvent )
{
CCTouch* touch=(CCTouch*)pTouches->anyObject();
CCPoint touchPoint=touch->getLocation();
for (int i=0;i<THREE;i++)
{
if (lotus[i]->boundingBox().containsPoint(touchPoint))
{
touchTag=i;
}
}
}
void U3BGameScene::ccTouchesMoved( CCSet *pTouches, CCEvent *pEvent )
{
CCTouch* touch=(CCTouch*)pTouches->anyObject();
CCPoint touchPoint=touch->getLocation();
for (int i=0;i<THREE;i++)
{
if (lotus[i]->boundingBox().containsPoint(touchPoint)&&touchTag==i)
{
lotus[i]->setPosition(touchPoint);
label[i]->setPosition(touchPoint);
}
}
}
void U3BGameScene::ccTouchesEnded( CCSet *pTouches, CCEvent *pEvent )
{
CCTouch* touch=(CCTouch*)pTouches->anyObject();
CCPoint touchPoint=touch->getLocation();
for (int i=0;i<THREE;i++)
{
if (lotus[i]->boundingBox().intersectsRect(CCRect(510,80,150,150)))
{
this->setTouchEnabled(false);
lotus[i]->setPosition(ccp(595,135));
label[i]->setPosition(ccp(595,135));
if (getAnswer(vRand[gameNum])==i)
{
rightHandler(i);
}
else
wrongHandler(i);
}
else
{
lotus[i]->setPosition(mypoint[posit[i]]);
label[i]->setPosition(mypoint[posit[i]]);
}
}
}
第四点、点击的回调函数
void menuClick(CCObject *pSender);
void U3BGameScene::menuClick(CCObject *pSender)
{
CCFadeIn* fadeIn = CCFadeIn::create(0.5f);
CCScaleTo* scaleToBig = CCScaleTo::create(0.5f,1.0f,1.0f);
CCFiniteTimeAction* spawnToBigCard = CCSpawn::create(fadeIn,scaleToBig,NULL);
CCMenuItemImage* getSender = (CCMenuItemImage*)pSender;
switch(getSender->getTag())
{
case 9999:{
CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(false);
CCScene *pScene = U3BScene_start::scene();
CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.2f,pScene));
break;
}
}
}
第五、定时函数。每过t时间就执行此函数
void timeHandler(float t);
使用方法为schedule(schedule_selector(U3BSceneGame::timeHandler),3.0f);
void U3BGameScene::timeHandler(float t)
{
hour--;
sprintf(game_time,"%d",hour);
pLabel_time->setString(game_time);
///<时间结束进入失败界面>
if (hour<=0)
{
//state =false;
failResult();
}
}
如果只是延时,则在函数里面判断停止条件然后执行unschedule(schedule_selector(U3BGameScene::timeHandler));即可。