实习小白::cocos2d-x 2.2 study ---------- 长按事件处理( 自己制作的例子 )
长按事件处理:
1. 设计一个定时器,用来延迟执行一个函数,就是弹框函数
2. 在点击事件的开始类型中,调用这个延迟函数,并规定事件,本例子是0.5s后弹出提示框,
并记录下点击的位置和点击创建提示框的标记,并把点击的那个图片获取到,也将其携带的内容获取到
3. 在移动事件中,获取移动的直线距离,若是超出了规定的距离,则停止调用计时器
4. 在结束事件中,停止计时器,并并判断是否创建了弹框,若有弹框,则将其删除
HelloWorld.h
1 #ifndef __HELLOWORLD_SCENE_H__ 2 #define __HELLOWORLD_SCENE_H__ 3 4 #include "cocos2d.h" 5 // 引入使用cocosStudio工具的头文件 6 #include "cocos-ext.h" 7 8 USING_NS_CC_EXT; 9 using namespace cocos2d::ui; 10 11 class HelloWorld : public cocos2d::CCLayer 12 { 13 public: 14 // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone 15 virtual bool init(); 16 17 // there's no 'id' in cpp, so we recommend returning the class instance pointer 18 static cocos2d::CCScene* scene(); 19 20 // a selector callback 21 void menuCloseCallback(CCObject* pSender); 22 23 // implement the "static node()" method manually 24 CREATE_FUNC(HelloWorld); 25 26 private: 27 // -- silent 28 void touchCallBack( CCObject *pSender, TouchEventType type ); // 点击图片时的回调函数 29 void createTipLayer( float dt ); // 要创建的提示框(参数可以根据需要修改) 30 bool touchLongFlag; // 是否创建了提示框 31 cocos2d::CCNode *nodeTipLayer; // 提示框 32 cocos2d::CCObject *object; // 提示框 33 cocos2d::CCPoint touchPointBegin; // 点击开始时的位置 34 cocos2d::CCPoint touchPointMove; // 点击移动时的位置 35 36 }; 37 38 #endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp
1 #include "HelloWorldScene.h" 2 3 USING_NS_CC; 4 5 CCScene* HelloWorld::scene() 6 { 7 // 'scene' is an autorelease object 8 CCScene *scene = CCScene::create(); 9 10 // 'layer' is an autorelease object 11 HelloWorld *layer = HelloWorld::create(); 12 13 // add layer as a child to scene 14 scene->addChild(layer); 15 16 // return the scene 17 return scene; 18 } 19 20 // on "init" you need to initialize your instance 21 bool HelloWorld::init() 22 { 23 ////////////////////////////// 24 // 1. super init first 25 if ( !CCLayer::init() ) 26 { 27 return false; 28 } 29 30 CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); 31 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); 32 33 // 获取资源中的json文件 34 cocos2d::ui::Widget *widget = cocos2d::extension::GUIReader::shareReader() -> widgetFromJsonFile( "player_touch_schedule/player_touch_schedule.json" ); 35 36 cocos2d::ui::UILayer *uiLayer = cocos2d::ui::UILayer::create(); // 创建一个 UiLayer 用于将 Widget 添加在这个层上 37 uiLayer -> addWidget( widget ); // 将json文件的 Widget 添加到 UILayer 中,然后可以使用UIlayer获取其中的任意子对象 38 this -> addChild( uiLayer ); 39 40 cocos2d::ui::UIImageView *bg = dynamic_cast<cocos2d::ui::UIImageView *>(uiLayer -> getWidgetByName( "dyimg_bg" )); 41 42 cocos2d::ui::UIImageView *touchBg = dynamic_cast<cocos2d::ui::UIImageView *>(uiLayer -> getWidgetByName( "dyimg_touchbg" )); 43 touchBg -> addTouchEventListener( this, toucheventselector(HelloWorld::touchCallBack) ); // 添加事件 44 touchBg -> setTouchEnabled( true ); // 设置可以发生触摸事件 45 touchBg -> setUserData( (void *) 1 ); // 设置内容(可以是任何数据,但是设置的时候需要将其类型转换为(void *)),读取的时候需要强转回来 46 47 cocos2d::ui::UIImageView *headBg = dynamic_cast<cocos2d::ui::UIImageView *> (uiLayer -> getWidgetByName( "dyimg_headbg")); 48 headBg -> addTouchEventListener( this, toucheventselector(HelloWorld::touchCallBack) ); 49 headBg -> setTouchEnabled( true ); 50 headBg -> setUserData( (void *) 2 ); 51 52 return true; 53 } 54 55 void HelloWorld::touchCallBack( CCObject *pSender, TouchEventType type ){ 56 57 // 在此函数中获取点击的那个图片,通过图片所携带的数据 58 switch ( type ) 59 { 60 case TOUCH_EVENT_BEGAN: 61 { 62 object = pSender; // 获取所点击的那个图片 63 int num = (int) ((CCSprite *)object) -> getUserData(); // 获取点击图片所携带的数据 64 touchLongFlag = false; // 还没有创建提示框 65 touchPointBegin = ( (cocos2d::ui::UIImageView *)pSender ) -> getTouchStartPos(); // 点击开始时的位置 66 // 在按下的0.5s之后创建一个提示框 67 this->scheduleOnce ( schedule_selector( HelloWorld::createTipLayer ), 0.5f ); 68 } 69 break; 70 case TOUCH_EVENT_MOVED: 71 { 72 touchPointMove = ( (cocos2d::ui::UIImageView *)pSender ) -> getTouchMovePos(); // 点击移动时的位置 73 // 求出移动的直线距离 74 float distance = sqrt( pow ( ( touchPointBegin.x - touchPointMove.x ), 2 ) 75 + pow ( ( touchPointBegin.y - touchPointMove.y ), 2 )); 76 if ( 50 < distance ) // 移动距离超出 50 的话 就将提示框移除 77 this -> unschedule ( schedule_selector( HelloWorld::createTipLayer ) ); 78 } 79 break; 80 case TOUCH_EVENT_ENDED: 81 { 82 this -> unschedule( schedule_selector( HelloWorld::createTipLayer ) ); 83 if ( touchLongFlag ) 84 this -> removeChild( nodeTipLayer ); 85 } 86 break; 87 case TOUCH_EVENT_CANCELED: 88 { 89 this -> unschedule( schedule_selector( HelloWorld::createTipLayer ) ); 90 if ( touchLongFlag ) 91 this -> removeChild( nodeTipLayer ); 92 } 93 break; 94 } 95 } 96 97 void HelloWorld::createTipLayer( float dt ){ 98 touchLongFlag = true; // 标记已经添加了提示框 99 // 要创建的提示框,这里用一张图片代替 100 nodeTipLayer = CCSprite::create("bd_xin01.png"); 101 nodeTipLayer -> setPosition( ((cocos2d::ui::UIImageView *)object)->getWorldPosition() ); 102 nodeTipLayer -> setAnchorPoint( ccp(1, 0) ); 103 this -> addChild( nodeTipLayer ); 104 } 105 106 void HelloWorld::menuCloseCallback(CCObject* pSender) 107 { 108 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) 109 CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); 110 #else 111 CCDirector::sharedDirector()->end(); 112 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 113 exit(0); 114 #endif 115 #endif 116 }
结果: