搜索
查看: 629|回复: 0
打印 上一主题 下一主题

【cocos2d-x】猜数字游戏

[复制链接]
跳转到指定楼层
楼主
发表于 2016-5-28 16:21:56 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
这个帖子是笔记,有想用C或C++语言开发游戏的可以加这个
掌柜的创的Cocos2d-x 学习群:153412944

C++代码:

//HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d:ayer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
private:
    cocos2d::Size visibleSize;
    int theRandomNum;
    void buildUI();
    void addListeners();
    cocos2d::TextFieldTTF * tf;
    cocos2d:abel * submitBtn, * messageLabel;
};
#endif // __HELLOWORLD_SCENE_H__
//HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace cocostudio::timeline;
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()
{
    if ( !Layer::init() )
    {
        return false;
    }

    visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    srand(time(NULL));
    theRandomNum = rand()%100;
    log("The random num is %d",theRandomNum);
    buildUI();
    addListeners();
    return true;
}
void HelloWorld::buildUI()
{
    auto label = Label::create();
    label->setString("lease 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();
    addChild(messageLabel);
    messageLabel->setPosition(visibleSize.width / 2, submitBtn->getPositionY() - 50 );
}
void HelloWorld::addListeners()
{
    auto director = Director::getInstance();
    auto handler = [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();
                //log("Submit");
                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("Input value is right");
                }
            }
        }
        else
        {
            tf->detachWithIME();
        }
        return false;
    };
    auto L = EventListenerTouchOneByOne::create();
    L->onTouchBegan = handler;
    director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(L, tf);
    auto submitBtnClickListener = EventListenerTouchOneByOne::create();
    submitBtnClickListener->onTouchBegan = handler;

    director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(submitBtnClickListener, submitBtn);
}


效果图:




是不是很奇怪,猜了三次就猜对了~!其实是看了这行代码才知道那个随机数是几的
log("The random num is %d",theRandomNum);


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表