// Created by Jacedy on 14-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();
Point origin = Director::getInstance()->getVisibleOrigin();
auto label = Label::createWithSystemFont( "Hello World" , "Mark Felt" , 25);
label->setPosition(Point(origin.x + size.width / 2, origin.y + size.height - label->getContentSize().height));
this ->addChild(label, 1);
//EditBox
auto inputBox = Sprite::create( "YellowSquare.png" );
inputBox->setPosition(Point(size.width/2, size.height/3));
this ->addChild(inputBox, 1);
EditBox *editBox = EditBox::create(Size(100, 35.0), Scale9Sprite::create());
editBox->setPosition(Point(size.width/2, size.height/3));
//editBox->setPlaceHolder("请输入数字"); //设置editBox输入为空时的显示状态
editBox->setInputMode(EditBox::InputMode::NUMERIC); //输入模式,这里设置为数字
editBox->setDelegate( this ); //开启委托
editBox->setFontColor(Color3B::BLACK);
editBox->setText( "0" );
editBox->setTag(10);
this ->addChild(editBox, 2);
//ControlSlider
auto slide_control = ControlSlider::create( "sliderProgress.png" , "sliderTrack.png" , "sliderThumb.png" );
slide_control->setPosition(Point(size.width/2, size.height/2));
slide_control->setMinimumValue(0.0f); //设置最小值
slide_control->setMaximumValue(100.0f); //设置最大值
slide_control->setValue(0.0f); //设置初始值
slide_control->setTag(20);
//设置拖动回调
slide_control->addTargetWithActionForControlEvents( this , cccontrol_selector(GameScene::sliderCallback), Control::EventType::VALUE_CHANGED);
this ->addChild(slide_control, 2);
return true ;
}
void GameScene::editBoxEditingDidBegin(EditBox *editBox)
{
}
void GameScene::editBoxEditingDidEnd(EditBox *editBox)
{
}
void GameScene::editBoxTextChange(EditBox *editBox, const std::string &text)
{
int num_int = std:: atoi (text.c_str()); //将字符串转成整型
int maxNum = 100;
if (num_int > maxNum) {
num_int = maxNum;
}
char buf_str[16];
sprintf (buf_str, "%d" , num_int);
editBox->setText(buf_str); //重置editBox的值
//改变ControlSlider的值
auto slide = (ControlSlider *) this ->getChildByTag(20);
slide->setValue(( float )num_int); //重置slide的值
}
void GameScene::editBoxReturn(EditBox *editBox)
{
}
void GameScene::sliderCallback(Ref *sender, Control::EventType controlEvent)
{
auto slide_control = (ControlSlider *)sender; //通过回调参数sender获得ControlSlider
int current_value = slide_control->getValue(); //获得slide当前的值
char buf_str[16];
sprintf (buf_str, "%d" , current_value);
auto editBox = (EditBox *) this ->getChildByTag(10);
editBox->setText(buf_str);
}
|