Cocos2d-x's guess number game

Source: Internet
Author: User
Tags addchild random seed

BOOL Helloworld::init ()

{

if (! Layer::init ())

{

return false;

}

Visiblesize = Director::getinstance ()->getvisiblesize ();

VEC2 origin = Director::getinstance ()->getvisibleorigin ();

Srand (Time (NULL));//Random Seed

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

}

Cocos2d-x's guess number game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.