首页 > 代码库 > cocos2dx 3.x(定时器或延时动作自动调用button的点击响应事件)实现自动内测
cocos2dx 3.x(定时器或延时动作自动调用button的点击响应事件)实现自动内测
1 // 2 // ATTGamePoker.hpp 3 // MalaGame 4 // 5 // Created by work on 2016/11/09. 6 // 7 // 8 9 #ifndef ATTGamePoker_hpp 10 #define ATTGamePoker_hpp 11 12 #include <stdio.h> 13 #include <cocos2d.h> 14 15 16 class ATTGamePoker : public cocos2d::Layer 17 { 18 19 private: 20 21 cocos2d::ui::Button * m_btnOne;//按钮 22 23 cocos2d::ui::Button * m_btnTow; 24 public: 25 26 virtual bool init(); 27 28 void oneCB(Ref *pSender, cocos2d::ui::Widget::TouchEventType type);//声明点击事件 29 void towCB(Ref *pSender, cocos2d::ui::Widget::TouchEventType type); 30 31 32 void one(float dt);//定时器调用 33 34 void Tow();//动画延时调用 35 36 37 CREATE_FUNC(ATTGamePoker); 38 39 }; 40 41 #endif /* ATTGamePoker_hpp */
1 // 2 // ATTGamePoker.cpp 3 // ATT 4 // 5 // Created by work on 16/10/13. 6 // 7 // 8 9 #include "ATTGamePoker.hpp" 10 #include "SimpleAudioEngine.h" 11 12 USING_NS_CC; 13 14 Scene* ATTGamePoker::createScene() 15 { 16 // ‘scene‘ is an autorelease object 17 auto scene = Scene::create(); 18 19 // ‘layer‘ is an autorelease object 20 auto layer = ATTGamePoker::create(); 21 22 // add layer as a child to scene 23 scene->addChild(layer); 24 25 // return the scene 26 return scene; 27 } 28 29 30 bool ATTGamePoker::init() 31 { 32 33 34 if ( !Layer::init() ) 35 { 36 return false; 37 } 38 39 40 41 42 auto soundbg=cocos2d::ui::Scale9Sprite::createWithSpriteFrameName("gobang_option_sound_off.png"); 43 soundbg->setScale(0.667); 44 m_btnOne=ControlButton::create(soundbg); 45 m_btnOne->setPreferredSize(cocos2d::Size(162,58)); 46 m_btnOne->setPosition(getPoint(1136, 627)); 47 m_btnOne->addTargetWithActionForControlEvents(this, cccontrol_selector(ATTGamePoker::oneCB), Control::EventType::TOUCH_UP_INSIDE); 48 this->addChild(m_btnOne); 49 50 51 52 53 auto soundbg2=cocos2d::ui::Scale9Sprite::createWithSpriteFrameName("gobang_option_sound_off.png"); 54 soundbg2->setScale(0.667); 55 m_btnTow=ControlButton::create(soundbg2); 56 m_btnTow->setPreferredSize(cocos2d::Size(162,58)); 57 m_btnTow->setPosition(getPoint(1036, 627)); 58 m_btnTow->addTargetWithActionForControlEvents(this, cccontrol_selector(ATTGamePoker::towCB), Control::EventType::TOUCH_UP_INSIDE); 59 this->addChild(m_btnTow); 60 61 62 63 this-> schedule(schedule_selector(ATTGamePoker::one), 10.0);//10秒调用一次 64 65 66 this->runAction(Sequence::create(DelayTime::create(5.0f),CallFunc::create(CC_CALLBACK_0(ATTGamePoker::Tow, this)), NULL));//动作延时调用,5秒一次 67 68 69 70 // 71 72 return true; 73 } 74 75 // 实现延时调用方法 76 void ATTGamePoker:: one() 77 { 78 this->oneCB(this,Widget::TouchEventType::ENDED);//调用事件时传当前的this,和判断的点击类型 79 } 80 81 void ATTGamePoker:: tow()/***g得分**/ 82 { 83 this->towCB(this,Widget::TouchEventType::ENDED); 84 } 85 86 87 88 89 // 实现按钮点击调用方法 90 void ATTGamePoker::oneCB(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type) 91 { 92 if (type != Widget::TouchEventType::ENDED)//判断当前的点击类型,按钮按下松开生效,执行打印语句,否则直接return,不执行下面的打印语句 93 { 94 return; 95 } 96 97 CCLOG("定时器调用了此方法"); 98 } 99 100 101 102 void ATTGamePoker::towCB(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type) 103 { 104 if (type != Widget::TouchEventType::ENDED) 105 { 106 return; 107 } 108 109 CCLOG("动作延时调用了此方法"); 110 }
cocos2dx 3.x(定时器或延时动作自动调用button的点击响应事件)实现自动内测
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。