cocos2dx 3.x ControlButton的认识

cocos2dx 3.x ControlButton的认识,第1张

概述1. ControlButton的介绍 ControlButton按钮的大小可以根据标签内容进行缩放,同时它具有很多按钮所需要的功能。 2.  ControlButton的使用 [html]  view plain copy print ? <span style="white-space:pre">    </span>//正常状态下的按钮图片       Scale9Sprite* btnNo 1. Controlbutton的介绍

Controlbutton按钮的大小可以根据标签内容进行缩放,同时它具有很多按钮所需要的功能。

2. Controlbutton的使用

[HTML] view plain copy print ? <spanstyle="white-space:pre"></span>//正常状态下的按钮图片 Scale9Sprite*btnnormal=Scale9Sprite::create("button.png"); //单击状态下的按钮图片 Scale9Sprite*btnPress=Scale9Sprite::create("buttonHighlighted.png"); //按钮标题 LabelTTF*Title=LabelTTF::create("touchme!","MarkerFelt",30); //创建按钮,按钮的大小根据标题自动调整 Controlbutton*btn=Controlbutton::create(Title,btnnormal); //设置按钮按下时的图片 btn->setBackgroundSpriteForState(btnPress,Control::State::SELECTED); //强制设置按钮大小,如果按钮超过这个范围,则自动扩大 btn->setPreferredSize(Size(300,50)); >setposition(spanstyle="Font-family:Arial,sans-serif;">Point>(200,200)); this->addChild(btn);
btn->setBackgroundSpriteForState(btnPress,Control::State::SELECTED);

State:norMAL、HIGH_liGHTED 、Disabled、SELECTED

norMAL=1<<0,//Thenormal,ordefaultstateofacontrol—thatis,enabledbutneitherselectednorhighlighted. HIGH_liGHTED=11,//Highlightedstateofacontrol.Acontrolentersthisstatewhenatouchdown,draginsIDeordragenterisperformed.YoucanretrIEveandsetthisvaluethroughthehighlightedproperty. Disabled=12,//Disabledstateofacontrol.ThisstateindicatesthatthecontroliscurrentlyDisabled.YoucanretrIEveandsetthisvaluethroughtheenabledproperty. SELECTED=13//Selectedstateofacontrol.Thisstateindicatesthatthecontroliscurrentlyselected.YoucanretrIEveandsetthisvaluethroughtheselectedproperty. 个人暂时不太了解这几个状态的用处,日后补充


3. 按钮事件

playMusic.h

#ifndef__PLAYMUSIC_H__ #define__PLAYMUSIC_H__ #include"cocos2d.h" #include"extensions/cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; classplayMusic:publiccocos2d::Layer{ public: staticcocos2d::Scene*createScene(); virtualboolinit(); CREATE_FUNC(PlayMusic); voIDaddChildAt(Node*node,floatpercentageX,floatpercentageY); private: voIDtouchDown(Ref*pSender,Control::EventTypeevent); voIDdragInsIDe(Ref*pSender,Control::EventTypeevent); voIDdragOutsIDe(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> voIDdragenter(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> voIDdragExit(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> voIDtouchUpInsIDe(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> voIDtouchUpOutsIDe(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> voIDtouchCancel(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> voIDvalueChanged(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> }; #endif
playMusic.cpp

#include"playMusic.h" #include"extensions/cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; Scene*PlayMusic::createScene() { autoscene=Scene::create(); autolayer=PlayMusic::create(); scene->addChild(layer); returnscene; } boolPlayMusic::init() if(!Layer::init()) returnfalse; } //正常状态下的按钮图片 Scale9Sprite*btnnormal=Scale9Sprite::create("button.png"); //单击状态下的按钮图片 Scale9Sprite*btnPress=Scale9Sprite::create("buttonHighlighted.png"); //按钮标题 LabelTTF*Title=LabelTTF::create("touchme!",30); //创建按钮,按钮的大小根据标题自动调整 Controlbutton*btn=Controlbutton::create(Title,btnnormal); //设置按钮按下时的图片 //强制设置按钮大小,如果按钮超过这个范围,则自动扩大 >setposition(Point(200,200)); >addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::touchDown),Control::EventType::touch_DOWN); this->addChild(btn); returntrue; voidplayMusic::touchDown(Ref*pSender,Control::EventTypeevent) { log("touchdown"); voidplayMusic::dragInsIDe(Ref*pSender,Control::EventTypeevent) log("dragInsIDe"); voidplayMusic::dragOutsIDe(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("dragOutsIDe"); voidplayMusic::dragenter(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("dragenter"); voidplayMusic::dragExit(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("dragExit"); voidplayMusic::touchUpInsIDe(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("touchUpInsIDe"); voidplayMusic::touchUpOutsIDe(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("touchUpOutsIDe"); voidplayMusic::touchCancel(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("touchCancel"); voidplayMusic::valueChanged(Ref*pSender,108); List-style:decimal-leading-zero outsIDe; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("valueChanged"); }
touch_DOWN=1DRAG_INSIDE=1DRAG_OUTSIDE=1DRAG_ENTER=13,//Aneventwhereafingerisdraggedintotheboundsofthecontrol. DRAG_EXIT=14,//AneventwhereafingerisdraggedfromwithinacontroltooutsIDeitsbounds. touch_UP_INSIDE=15,//Atouch-upeventinthecontrolwherethefingerisinsIDetheboundsofthecontrol. touch_UP_OUTSIDE=16,//Atouch-upeventinthecontrolwherethefingerisoutsIDetheboundsofthecontrol. touch_CANCEL=17,//Asystemeventcancelingthecurrenttouchesforthecontrol. VALUE_CHANGED=18//Atouchdraggingorotherwisemanipulatingacontrol,causingittoemitaserIEsofdifferentvalues. touch_DOWN 按下按钮调用事件
DRAG_INSIDE
当处于按下并点中按钮的状态下,进入按钮范围,则触发,可以多次触发
DRAG_OUTSIDE
当处于按下并点中按钮的状态下,离开按钮范围,则触发,可以多次触发
DRAG_ENTER
当处于按下并点中按钮的状态下,进入按钮范围,则触发,一次
DRAG_EXIT
当处于按下并点中按钮的状态下,离开按钮范围,则触发,一次
touch_UP_INSIDE
当处于按下并点中按钮的状态下,鼠标松开且在按钮范围内,则触发,一次
touch_UP_OUTSIDE
当处于按下并点中按钮的状态下,鼠标松开且在按钮范围外,则触发,一次
touch_CANCEL
系统事件中断按钮事件而触发

VALUE_CHANGED 触摸拖曳或 *** 纵控制,让它发出一系列的不同的值。


原文来自:http://blog.csdn.net/lengxue789/article/details/38228935

总结

以上是内存溢出为你收集整理的cocos2dx 3.x ControlButton的认识全部内容,希望文章能够帮你解决cocos2dx 3.x ControlButton的认识所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066092.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存