cocos2d-x之猜数字游戏
2016-03-10 11:29
393 查看
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
srand(time(NULL));//随机种子
theRandomNum=rand()%100;//0~100之间
log("the randnum is %d",theRandomNum);
buildUI();
addListeners();
return true;
}
void HelloWorld::buildUI(){
auto label=Label::create();
label->setString("Please input a number between 0~100");
addChild(label);
label->setPosition(visibleSize.width/2,visibleSize.height-label->getContentSize().height/2-20);
tf=TextFieldTTF::textFieldWithPlaceHolder("Input number here","Courier",16);
tf->setPosition(visibleSize.width/2,label->getPositionY()-50);
addChild(tf);
submitBtn=Label::create();
submitBtn->setPosition(visibleSize.width/2,tf->getPositionY()-50);
submitBtn->setString("Submit");
addChild(submitBtn);
messageLabel=Label::create();
messageLabel->setPosition(visibleSize.width/2,submitBtn->getPositionY()-50);
addChild(messageLabel);
}
void HelloWorld::addListeners(){
auto director=Director::getInstance();
auto handle=[this](Touch* t,Event* e){
auto target=e->getCurrentTarget();
auto point=t->getLocation();
if (target->getBoundingBox().containsPoint(point)) {
if (target==tf) {
tf->attachWithIME();
}else if (target==submitBtn){
tf->detachWithIME();
int inputValue=__String::create(tf->getString())->intValue();
if (inputValue>theRandomNum) {
messageLabel->setString("input value is bigger");
}else if (inputValue<theRandomNum){
messageLabel->setString("input value is smaller");
}else{
messageLabel->setString("you get it");
}
}
}else{
tf->detachWithIME();
}
// if (e->getCurrentTarget()==tf) {
// tf->attachWithIME();
// }else if (e->getCurrentTarget()==submitBtn){
//
// }else{
// tf->detachWithIME();
// }
return false;
};
auto l=EventListenerTouchOneByOne::create();
l->onTouchBegan=handle;
director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(l,tf);
auto submitBtnClickListener=EventListenerTouchOneByOne::create();
submitBtnClickListener->onTouchBegan=handle;
director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(submitBtnClickListener,submitBtn);
}
{
if ( !Layer::init() )
{
return false;
}
visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
srand(time(NULL));//随机种子
theRandomNum=rand()%100;//0~100之间
log("the randnum is %d",theRandomNum);
buildUI();
addListeners();
return true;
}
void HelloWorld::buildUI(){
auto label=Label::create();
label->setString("Please input a number between 0~100");
addChild(label);
label->setPosition(visibleSize.width/2,visibleSize.height-label->getContentSize().height/2-20);
tf=TextFieldTTF::textFieldWithPlaceHolder("Input number here","Courier",16);
tf->setPosition(visibleSize.width/2,label->getPositionY()-50);
addChild(tf);
submitBtn=Label::create();
submitBtn->setPosition(visibleSize.width/2,tf->getPositionY()-50);
submitBtn->setString("Submit");
addChild(submitBtn);
messageLabel=Label::create();
messageLabel->setPosition(visibleSize.width/2,submitBtn->getPositionY()-50);
addChild(messageLabel);
}
void HelloWorld::addListeners(){
auto director=Director::getInstance();
auto handle=[this](Touch* t,Event* e){
auto target=e->getCurrentTarget();
auto point=t->getLocation();
if (target->getBoundingBox().containsPoint(point)) {
if (target==tf) {
tf->attachWithIME();
}else if (target==submitBtn){
tf->detachWithIME();
int inputValue=__String::create(tf->getString())->intValue();
if (inputValue>theRandomNum) {
messageLabel->setString("input value is bigger");
}else if (inputValue<theRandomNum){
messageLabel->setString("input value is smaller");
}else{
messageLabel->setString("you get it");
}
}
}else{
tf->detachWithIME();
}
// if (e->getCurrentTarget()==tf) {
// tf->attachWithIME();
// }else if (e->getCurrentTarget()==submitBtn){
//
// }else{
// tf->detachWithIME();
// }
return false;
};
auto l=EventListenerTouchOneByOne::create();
l->onTouchBegan=handle;
director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(l,tf);
auto submitBtnClickListener=EventListenerTouchOneByOne::create();
submitBtnClickListener->onTouchBegan=handle;
director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(submitBtnClickListener,submitBtn);
}
相关文章推荐
- 纹理缓存TextureCache-Cocos2dx
- cocos2dx TabelView 表格视图
- cocos2dx Menu 菜单
- cocos2dx 触摸事件
- cocos2dx LabelTTF 标签
- 骨骼动画详解-Spine
- cocos2d-x之加法计算器
- cocos2dx 无法打开包含文件extensions/ExtensionExport.h
- Cocos2d-x之文件操作
- cocos2dx--cocos2dx3.1.1执行报无法解析的外部符号
- cocos2dx打包apk
- Cocos2dx中零散知识点
- cocos2dx3.x 按钮 输入框
- Logcat 不显示日志的另一个原因. 跟cocos2dx关系不大.
- cocos2dx学习笔记之retain()和release()函数简介
- Android游戏包括Unity3D和Cocos2D-X退出不干净的问题
- cocos2d-x之悦动的小球
- cocos2d-js的自定义事件的两种创建方法
- cocos 中文字符显示问题
- cocos2d 场景切换特效