cocos2dx 3.x(Button传统按钮)

 1 //
 2 //  ATTLoagingScene.hpp
 3 //  ATT
 4 //
 5 //  Created by work on 16/10/13.
 6 //
 7 //
 8 
 9 #ifndef ATTLoagingScene_hpp
10 #define ATTLoagingScene_hpp
11 
12 #include <stdio.h>
13 #include "cocos2d.h"
14 #include "cocos-ext.h"//使用按钮事件,必须要需要的头文件
15 USING_NS_CC_EXT;//使用按钮事件,必须要需要的命名空间
16 
17 class ATTLoagingScene : public cocos2d::Layer
18 
19 {
20 
21    
22 private:
23 
24     cocos2d::ui::Button * m_soundOnButton;//私有的成员变量
25 
26     cocos2d::ui::Button * m_btnPlus;
27 
28       
29 
30 public:
31 
32     static cocos2d::Scene* createScene();    
33 
34     virtual bool init();
35 
36 //声明点击事件
37 
38     void menuCloseCallback(Ref *ref,cocos2d::extension::Control::EventType event);//对原来的关闭事件进行改造,增加参数,让其支持cccontrol_selector
39 
40     // implement the "static create()" method manually
41 
42     CREATE_FUNC(ATTLoagingScene);
43 
44 };
45 
46  
47 
48 #endif /* ATTLoagingScene_hpp */
 1 //
 2 //  ATTLoagingScene.cpp
 3 //  ATT
 4 //
 5 //  Created by work on 16/10/13.
 6 //
 7 //
 8 
 9 #include "ATTLoagingScene.hpp"
10 #include "SimpleAudioEngine.h"
11 #include "ATTGameScene.hpp"
12 USING_NS_CC;
13 
14 Scene* ATTLoagingScene::createScene()
15 {
16     // 'scene' is an autorelease object
17     auto scene = Scene::create();
18     
19     // 'layer' is an autorelease object
20     auto layer = ATTLoagingScene::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 ATTLoagingScene::init()
31 {
32     
33     
34     if ( !Layer::init() )
35     {
36         return false;
37     }
38 
39 
40 auto soundbg=cocos2d::ui::Scale9Sprite::createWithSpriteFrameName("gobang_option_sound_off.png");
41     soundbg->setScale(0.667);
42     m_soundOnButton=ControlButton::create(soundbg);
43     m_soundOnButton->setPreferredSize(cocos2d::Size(162,58));
44     m_soundOnButton->setPosition(getPoint(1136, 627));
45     m_soundOnButton->addTargetWithActionForControlEvents(this, cccontrol_selector(GoBangScene::buttonSoundOnCallBack), Control::EventType::TOUCH_UP_INSIDE);
46     this->addChild(m_soundOnButton,2);
47     m_soundOnButton->setVisible(false);//设置为隐藏,true为可见。
48     m_soundOnButton->setEnabled(false);//设置为禁用,true为可用。
49     m_soundOnButton->setOpacity(200);//透明度 setOpacity 透明度 0~255(取值范围),255不透明,0全透明 
50 
61    
62 
63 //
64 
65     return true;
66 }
67 
68 // 实现点击方法
69 #pragma mark - ATTGameScene::optionCB(设置按钮)
70 void ATTGameScene::buttonSoundOnCallBack(Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
71 {
72     /* 
73      *判断当前点击类型(只响应一次,松开时生效)
74      */
75     if (type != cocos2d::ui::Widget::TouchEventType::ENDED)
76     {
77         return;
78     }
79     // 直接取反 (判断当前节点是否被隐藏,若隐藏直接取反)
80     //m_optionNode->setVisible(!m_optionNode->isVisible());
81 
82 }
原文地址:https://www.cnblogs.com/luorende/p/5983189.html