//GameScene.h #include "cocos2d.h" class GameScene : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); void menuCallback(cocos2d::Ref* pSender); void update(float dt); void scheCallBack_1(float dt); void scheCallBack_2(float dt); CREATE_FUNC(GameScene); };
//GameScene.cpp //功能:定时器的不同使用方法 #include "GameScene.h" USING_NS_CC; cocos2d::Scene* GameScene::createScene() { auto scene = Scene::create(); //创建一个场景 auto layer = GameScene::create(); //创建一个图层 scene->addChild(layer); return scene; } //初始化当前的图层 bool GameScene::init() { if(!Layer::init()) //初始化父类 return false; //获取屏幕大小 Size visibleSize = Director::getInstance()->getVisibleSize(); //auto size = Director::getInstance()->getWinSize(); //添加一个图片精灵 auto sprite = Sprite::create("OnePiece_1.png"); sprite->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2)); this->addChild(sprite); auto label = Label::createWithSystemFont("0","Marker Felt",45); label->setAnchorPoint(Vec2::ZERO); label->setPosition(Vec2(0,visibleSize.height*0.5)); this->addChild(label); //定时器的使用 //方法一 schedule(schedule_selector(GameScene::scheCallBack_1)); //每一帧都会调用函数scheCallBack_1() //方法二 scheduleUpdate(); //每一帧都会调用函数update() //方法三 // schedule(schedule_selector(GameScene::scheCallBack_2),3); //每3秒调用一次函数scheduleCallBack_2() //方法四 // scheduleOnce(schedule_selector(GameScene::scheCallBack_2),2); //延时2秒后调用函数scheduleCallBack_2()一次 //方法五 // schedule(schedule_selector(GameScene::scheCallBack_2),3,5,1); //延时1秒后每隔3秒调用函数scheduleCallBack_2(),重复5次,总共调用6次 return true; } void GameScene::update(float dt) { // static int i=0; // log("Jacedy %d",i); // i++; } void GameScene::scheCallBack_1(float dt) { // static int i=0; // log("Jacedy %d",i); // i++; } void GameScene::scheCallBack_2(float dt) { static int i=0; log("Jacedy %d",i); i++; }原文链接:https://www.f2er.com/cocos2dx/342833.html