cocos2d-x C++ (利用定时器自定义屏幕双击事件函数)

 //GameScene.h

 #include "cocos2d.h"

 USING_NS_CC;

 class GameScene : public cocos2d::Layer

 {

 public:

     static cocos2d::Scene* createScene();

     virtual bool init();

     void singleClickEvent();

     void doubleClickEvent();

     void funCallback(float dt);

     virtual bool onTouchBegan(Touch *touch, Event *unused_event);

     CREATE_FUNC(GameScene);

 private:        //注意不能用auto关键字

     Size size;

     Sprite *sprite;

     bool isClicked;

 };
 //GameScene.cpp

 //  Created by Jacedy on 16-8-11.

 #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 = Director::getInstance()->getVisibleSize();

     //auto size = Director::getInstance()->getWinSize();

     //添加一个图片精灵

     sprite = Sprite::create("OnePiece_1.png");

     sprite->setPosition(Vec2(size.width/, size.height/));

     this->addChild(sprite);

     isClicked = false;

     //创建监听事件对象

     auto listener = EventListenerTouchOneByOne::create();

     //定义监听事件的回调函数

     listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);

     //在事件分发器中注册

     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

     return true;

 }

 void GameScene::singleClickEvent()

 {

     log("Single click");

 }

 void GameScene::doubleClickEvent()

 {

     log("Double click");

 }

 void GameScene::funCallback(float dt)

 {

     if (isClicked)

     {

         isClicked = false;

         singleClickEvent();

     }

 }

 bool GameScene::onTouchBegan(Touch *touch, Event *unused_event)

 {

     if (isClicked)

     {

         isClicked = false;

         doubleClickEvent();

     }

     else

     {

         isClicked = true;

         //0.3秒内再次点击将再次调用onTouchBegan函数,0.3秒后再次点击将调用funCallback函数

         scheduleOnce(schedule_selector(GameScene::funCallback), 0.3f);

     }

     return false;

 }
上一篇:芝麻HTTP:Python爬虫入门之正则表达式


下一篇:Flask(4)- flask请求上下文源码解读、http聊天室单聊/群聊(基于gevent-websocket)