搜索
查看: 1119|回复: 1
打印 上一主题 下一主题

【cocos2d-x】加法计算器

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

其实我就想知道1+1等于几,计算后原来是等于2,好简单哈!

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:
    void buildUI();
    void addListeners();
    cocos2d::TextFieldTTF * aTf, * bTf;
    cocos2d:abel * resultLabel, * addBtn;
};
#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()
{
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    buildUI();
    addListeners();
    return true;
}
void HelloWorld::buildUI()
{
    aTf = TextFieldTTF::textFieldWithPlaceHolder("Value","Courier",16);
    aTf->setPosition(100,200);
    addChild(aTf);
    auto addLabel = Label::create();
    addLabel->setString("+");
    addChild(addLabel);
    addLabel->setPosition(aTf->getPositionX() + 50, aTf->getPositionY());
    bTf = TextFieldTTF::textFieldWithPlaceHolder("Value","Courier",16);
    bTf-> setPosition(addLabel->getPositionX() + 50, aTf->getPositionY());
    addChild(bTf);
    auto equalLabel = Label::create();
    equalLabel->setString("=");
    addChild(equalLabel);
    equalLabel->setPosition(bTf->getPositionX()+50,bTf->getPositionY());

    resultLabel = Label::create();
    addChild(resultLabel);
    resultLabel->setPosition(equalLabel->getPositionX() + 50, equalLabel->getPositionY());
    addBtn = Label::create();
    addBtn->setString("add");
    addBtn->setSystemFontSize(16);
    addChild(addBtn);
    addBtn->setPosition(aTf->getPositionX(),aTf->getPositionY()-50);
}
void HelloWorld::addListeners()
{
    auto director = Director::getInstance();
    auto handler = [=](Touch * t,Event * e)
    {
        auto target = e->getCurrentTarget();
        if (target->getBoundingBox().containsPoint(t->getLocation()))
        {
            if (aTf == target)
            {
                aTf->attachWithIME();
            }
            else if (bTf == target)
            {
                bTf->attachWithIME();
            }
            else if (target == addBtn)
            {
                int a = __String::create(aTf->getString())->intValue();
                int b = __String::create(bTf->getString())->intValue();
                resultLabel->setString(StringUtils::format("%d",a + b));
            }
        }
        return false;
    };
    auto addListenerToTarget = [director, handler](Node * target)
    {
        auto I = EventListenerTouchOneByOne::create();
        I->onTouchBegan = handler;
        director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(I, target);
    };

    addListenerToTarget(addBtn);
    addListenerToTarget(aTf);
    addListenerToTarget(bTf);
}



效果图:


沙发
发表于 2016-5-27 20:51:42 | 只看该作者
好厉害,加油
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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