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);

}

最新文章

  1. AJAX JSONP源码实现(原理解析)
  2. BZOJ_1627_[Usaco2007_Dec]_穿越泥地_(bfs)
  3. Bios里,把SATA Mode Selection改为AHCI无法启动
  4. GDAL python教程(1)——用OGR读写矢量数据
  5. A Very Easy Triangle Counting Game
  6. 开始工作----微信通过get检查当前网站---是否可用
  7. Spring boot实现原生websocket
  8. 代码块: 以冒号作为开始,用缩进来划分作用域,这个整体叫做代码块,python的代码块可以提升整体的整齐度,提高开发效率
  9. [随笔][Java][总结][java 类型系统]
  10. Gradle nexus 解决开发机器不连网无法下载gradle插件问题
  11. 部署Tomcat及nginx负载均衡
  12. Kylin的简介与安装部署
  13. 比较完整的HIS系统解释(转载记录)
  14. 使用Mobx插件查看mobx的变量值
  15. Java——线程同步
  16. C++将整型数据转换成大端或小端存储顺序
  17. C#绑定数据
  18. Oracle Certified Java Programmer 经典题目分析(一)
  19. ASP.NET web.config中&lt;customErrors&gt;节点说明
  20. docker 安装mysql mongo

热门文章

  1. 用Qt写软件系列四:定制个性化系统托盘菜单
  2. asp.net mvc中包含webapi时,token失效产生302的解决方案
  3. 新的Visual C++代码优化器
  4. out 和 ref 参数修饰符
  5. MySQL服务器权限表
  6. 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup
  7. Jquery 前端模版
  8. Hibernate关联映射及高级查询
  9. java中的代码块执行顺序
  10. PHP学习笔记:利用百度api实现手机归属地查询