此次项目开发跟以往一样,总共分四个模块:
1.UI设计
2.生成随机数
3.用户交互
4.游戏判断
UI设计:
通过代码设计出一定的界面出来,我在代码中用了一个方法实现,即是加了一个bulidUI()函数进来,代码如下,
void HelloWorld::bulidUI()
{
label = Label::create("please input a number random 0 -100 ", "Courier", 10);
label->setPosition(visibleSize.width / 2, visibleSize.height - label->getContentSize().height);
addChild(label);
textTTF = TextFieldTTF::textFieldWithPlaceHolder("INPUT YOUR NAME","courier",25);
textTTF->setPosition(label->getPositionX(), label->getPositionY() - 50);
addChild(textTTF);
klick = Label::create();
klick->setString("submit");
klick->setSystemFontSize(25);
klick->setPosition(label->getPositionX(), textTTF->getPositionY() - 50);
addChild(klick);
message = Label::create();
message->setSystemFontSize(25);
message->setPosition(klick->getPositionX(),klick->getPositionY()-50);
addChild(message);
}
生成随机数
代码非常简单,两行而已:
srand(time(NULL));
theronNUM = rand()%100;
用户交互和游戏判断
我把用户交互和游戏判断放在了一个方法里,即listener();
void HelloWorld::addListener()
{
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 == textTTF)
{
textTTF->attachWithIME();
}
if (target == klick)//游戏判断
{
textTTF->detachWithIME();
auto inputValue = __String::create(textTTF->getString())->intValue();
if (inputValue > theronNUM)
{
message->setString("your number is bigger");
}
else if (inputValue < theronNUM)
{
message->setString("your number is smaller");
}
else
{
message->setString("your number is right");
}
}
}
return false;
};
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = handle;
director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,textTTF);
auto submitBtnklick = EventListenerTouchOneByOne::create();
submitBtnklick->onTouchBegan = handle;
director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(submitBtnklick, klick);
}
总的代码如下:
HelloWorldScene.h文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void bulidUI();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
void addListener();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
private:
cocos2d::Size visibleSize;
cocos2d::Label *label, *klick,*message;
cocos2d::TextFieldTTF *textTTF;
int theronNUM;
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp文件:
#include "HelloWorldScene.h"
# include "ImageScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
visibleSize = Director::getInstance()->getVisibleSize();
srand(time(NULL));
theronNUM = rand()%100;
bulidUI();
addListener();
return true;
};
void HelloWorld::bulidUI()
{
label = Label::create("please input a number random 0 -100 ", "Courier", 10);
label->setPosition(visibleSize.width / 2, visibleSize.height - label->getContentSize().height);
addChild(label);
textTTF = TextFieldTTF::textFieldWithPlaceHolder("INPUT YOUR NAME","courier",25);
textTTF->setPosition(label->getPositionX(), label->getPositionY() - 50);
addChild(textTTF);
klick = Label::create();
klick->setString("submit");
klick->setSystemFontSize(25);
klick->setPosition(label->getPositionX(), textTTF->getPositionY() - 50);
addChild(klick);
message = Label::create();
message->setSystemFontSize(25);
message->setPosition(klick->getPositionX(),klick->getPositionY()-50);
addChild(message);
}
void HelloWorld::addListener()
{
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 == textTTF)
{
textTTF->attachWithIME();
}
if (target == klick)//游戏判断
{
textTTF->detachWithIME();
auto inputValue = __String::create(textTTF->getString())->intValue();
if (inputValue > theronNUM)
{
message->setString("your number is bigger");
}
else if (inputValue < theronNUM)
{
message->setString("your number is smaller");
}
else
{
message->setString("your number is right");
}
}
}
return false;
};
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = handle;
director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,textTTF);
auto submitBtnklick = EventListenerTouchOneByOne::create();
submitBtnklick->onTouchBegan = handle;
director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(submitBtnklick, klick);
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}