其实完成加法计算器很简单,大概分成三个步骤: 1.设计界面 2.用户交互 3.计算结果
大家一看代码便知,不懂得可以在评论区中问我!
HelloWorld.h代码:
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer { private: cocos2d::TextFieldTTF *aTF, *bTF; cocos2d ::Label * resultLabel,*addBtn; public: static cocos2d::Scene* createScene(); virtual bool init(); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender); void bulidUI(); void addListeners(); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__HelloWorld.cpp的代码:
#include "HelloWorldScene.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; } bulidUI(); addListeners(); return true; } void HelloWorld::bulidUI()//布局 { aTF = TextFieldTTF::textFieldWithPlaceHolder("value", "Courier", 25); aTF->setPosition(100,200); addChild(aTF); auto addLabel = Label::create(); addLabel->setString("+"); addLabel->setSystemFontSize(25); addLabel->setPosition(aTF->getPositionX()+50,aTF->getPositionY()); addChild(addLabel); bTF = TextFieldTTF::textFieldWithPlaceHolder("value","Courier",25); bTF->setPosition(aTF->getPositionX() + 100, aTF->getPositionY()); addChild(bTF); auto equalLabel = Label::create(); equalLabel->setString("="); equalLabel->setSystemFontSize(25); equalLabel->setPosition(aTF->getPositionX()+150,aTF->getPositionY()); addChild(equalLabel); resultLabel = Label::create(); resultLabel->setSystemFontSize(25); resultLabel->setPosition(equalLabel->getPositionX()+50,equalLabel->getPositionY()); addChild(resultLabel); addBtn = Label::create(); addBtn->setString("ADD"); addBtn->setSystemFontSize(25); addBtn->setPosition(aTF->getPositionX(),aTF->getPositionY()-50); addChild(addBtn); } 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 (addBtn == target) { 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 l = EventListenerTouchOneByOne::create();//创建事件监听器 l->onTouchBegan = handler; director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(l,target); }; addListenerToTarget(aTF); addListenerToTarget(bTF); addListenerToTarget(addBtn); } void HelloWorld::menuCloseCallback(Ref* pSender) { Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }运行结果如下: