티스토리 뷰


HelloWorldScene.h
  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3.  
  4. #include "cocos2d.h"
  5. #include "cocostudio/CocoStudio.h"
  6. #include "ui\UIEditBox\UIEditBox.h"
  7. #include "ui/CocosGUI.h"
  8.  
  9. USING_NS_CC;
  10. using namespace ui;
  11.  
  12. class HelloWorld : public cocos2d::Layer, EditBoxDelegate
  13. {
  14. public:
  15. // there's no 'id' in cpp, so we recommend returning the class instance pointer
  16. static cocos2d::Scene* createScene();
  17.  
  18. // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
  19. virtual bool init();
  20.  
  21. // a selector callback
  22. void menuCloseCallback(cocos2d::Ref* pSender);
  23.  
  24. // implement the "static create()" method manually
  25. CREATE_FUNC(HelloWorld);
  26.  
  27. EditBox* eb_first; //첫번째 에디트박스
  28. EditBox* eb_second; //두번째 에디트박스
  29.  
  30. void editbox_Callback(EditBox* editBox); //콜백받으면 실행될 임의 함수
  31.  
  32. virtual void editBoxEditingDidBegin(EditBox* editBox);
  33. virtual void editBoxEditingDidEnd(EditBox* editBox);
  34. virtual void editBoxTextChanged(EditBox* editBox, const std::string& text);
  35. virtual void editBoxReturn(EditBox* editBox);
  36. };
  37.  
  38. #endif // __HELLOWORLD_SCENE_H__
  39.  


HelloWorldScene.cpp

  1. #include "HelloWorldScene.h"
  2.  
  3. Scene* HelloWorld::createScene()
  4. {
  5. // 'scene' is an autorelease object
  6. auto scene = Scene::create();
  7.  
  8. // 'layer' is an autorelease object
  9. auto layer = HelloWorld::create();
  10.  
  11. // add layer as a child to scene
  12. scene->addChild(layer);
  13.  
  14. // return the scene
  15. return scene;
  16. }
  17.  
  18. // on "init" you need to initialize your instance
  19. bool HelloWorld::init()
  20. {
  21. //////////////////////////////
  22. // 1. super init first
  23. if ( !Layer::init() )
  24. {
  25. return false;
  26. }
  27.  
  28. auto rootNode = CSLoader::createNode("HelooWorldScene.csb");
  29. addChild(rootNode);
  30.  
  31. ///////////////////////////////////////첫번째 에디트박스///////////////////////////////////////
  32. eb_first = EditBox::create(Size(150, 50), Scale9Sprite::create("임의.png")); //에디트박스 생성
  33. eb_first->setAnchorPoint(Vec2(0, 0.5f)); //중심점을 좌측 중앙으로 설정
  34. eb_first->setPosition(Vec2(50, 100)); //위치설정
  35. eb_first->setReturnType(ui::EditBox::KeyboardReturnType::NEXT); //search버튼을 다음버튼으로 표시
  36. eb_first->setDelegate(this);
  37. addChild(eb_first); //에디트박스를 rootNode의 차일드로 추가
  38. ///////////////////////////////////////////////////////////////////////////////////////////////
  39. ///////////////////////////////////////두번째 에디트박스///////////////////////////////////////
  40. eb_second = EditBox::create(Size(150, 50), Scale9Sprite::create("임의.png")); //에디트박스 생성
  41. eb_second->setAnchorPoint(Vec2(0, 0.5f)); //중심점을 좌측 중앙으로 설정
  42. eb_second->setPosition(Vec2(250, 100)); //위치설정
  43. eb_second->setReturnType(ui::EditBox::KeyboardReturnType::DONE); //search버튼을 완료버튼으로 표시
  44. eb_second->setDelegate(this);
  45. addChild(eb_second); //에디트박스를 rootNode의 차일드로 추가
  46. ///////////////////////////////////////////////////////////////////////////////////////////////
  47.  
  48. return true;
  49. }
  50.  
  51. void HelloWorld::editBoxEditingDidBegin(EditBox* editBox) { //에디트 박스 클릭시 발생
  52. }
  53. void HelloWorld::editBoxEditingDidEnd(EditBox* editBox) { //에디트 박스에서 키보드 입력이 끝날때 발생
  54. }
  55. void HelloWorld::editBoxTextChanged(EditBox* editBox, const std::string& text) { //에디트 박스에서 글자 입력할때 마다 발생
  56. }
  57. void HelloWorld::editBoxReturn(EditBox* editBox) {
  58. //에디트 박스에서 리턴 키를 입력할 때 출력되는 메소드
  59. //일반적으로 editBoxEditingDidEnd() 호출후에 호출된다.
  60. if (editBox == eb_first) { //리턴키를 누른 에디트박스가 eb_first이면
  61. CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(HelloWorld::editbox_Callback, this, eb_second));
  62. this->runAction(Sequence::create(DelayTime::create(0.15f), runCallback, nullptr));
  63.          //0.15초의 딜레이 후->runCallback함수실행
  64. }
  65. }
  66. void HelloWorld::editbox_Callback(EditBox* editBox) { //콜백함수
  67. editBox->touchDownAction(editBox, Widget::TouchEventType::ENDED);
  68. //eb_second가 터치된것처럼 포커스를 얻고 키보드가 나타남
  69. }

에디트박스를 2개 만들어서

첫 에디트박스의 입력이 끝나면 자동으로 두번째 에디트박스로

이동되고 키보드가 나오게 하는 코드입니다.


딜레이를 넣지않으면 키보드가 안뜨거나

커서가 정신을 못차리고 왔다갔다하는 버그가 발생합니다.


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함