cocos2dx引擎 游戏开头logo自动跳转(定时器的使用)

装载自:http://www.cocoachina.com/bbs/read.php?tid=282508

摘自网络:

  1. void CCNode::schedule(SEL_SCHEDULE selector)  
  2.  
  3.     this->schedule(selector, 0.0f, kCCRepeatForever, 0.0f);  
  4.  
  5.   
  6. void CCNode::schedule(SEL_SCHEDULE selector, float interval)  
  7.  
  8.     this->schedule(selector, interval, kCCRepeatForever, 0.0f);  
  9.  
  10.   
  11. void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)  
  12.  
  13.     CCAssert( selector, "Argument must be non-nil");  
  14.     CCAssert( interval >=0, "Argument must be positive");  
  15.   
  16.     m_pScheduler->scheduleSelector(selector, thisinterval, !m_bIsRunning, repeat, delay);  
  17.  

简单的说明一下schedule,有重载,一种是一个参数,需要引入一个回调函数,另一种是两个参数,在回调函数的基础上,再加入一个间隔时间interval(这里注意一下,本人使用的是cocos2dx2.0.4版本,以前的版本间隔时间用的是CCTime类型,2.0.4用的是float类型),第三种是四个参数,依次是回调,间隔,重复次数,多少时间后调用这个函数。有了schedule还不够,明显的,我们还需要一个回调:

[cpp]  view plain  copy
 print ?
  1. void Step(float dt);   //2.0.4版本的亲们注意这里的dt的类型,网上有些老版本用的是CCTime  
  2. {  
  3.     /...../  
  4. }  


在这里回调里写上你要跳转的scene就ok了。


下面具体看一个例子。

先在AppDelegate.cpp文件中的applicationDidFinishLaunching()里修改一下,来进入游戏的一个画面——logo

[cpp]  view plain  copy
 print ?
  1. // create a scene. it's an autorelease object  
  2. CCScene *pScene = Logo::scene();  
  3.   
  4. // run  
  5. pDirector->runWithScene(CCTransitionFade::create(2.0f,pScene));  


然后在Logo 类中写好一个step回调函数:


[cpp]  view plain  copy
 print ?
  1. void Logo::Step(float dt)  
  2. {  
  3.     this->unschedule(schedule_selector(Logo::Step));  
  4.     CCDirector::sharedDirector()->purgeCachedData();  
  5.     CCScene *pScene = PMenu::scene();  
  6.     CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(2.0f,pScene));  
  7. }  

最后在Logo类的init函数中调用schedule:

[cpp]  view plain  copy
 print ?
  1. bool Logo::init()  
  2. {  
  3.     CCSprite * sprite = CCSprite::create("HelloWorld.png");  
  4.     sprite->setPosition(ccp(240, 160));    
  5.     this->addChild(sprite, 0);  
  6.     this->schedule(schedule_selector(Logo::Step), 3.0f);  
  7.     return true;  
  8. }  



ok这样就可以试试了:

logo淡入:





自动跳出,淡出:









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值