Internship Small white:: (EXT) Cocos2d-x 3.0 Development (15) using the Uilayout layout, create a dialogue interface

Source: Internet
Author: User

1. Overview

In the previous article we designed a static uiscrollview in the editor, and usually we need to add information dynamically in the program. How is the position of the inserted element determined? In 3.0 Uilayout has implemented the basic layout, take a look at it:

2. Editing interface

Open Cocostudio uieditor Edit an interface to create a ScrollView and two button. Because ScrollView inherits from Uilayout, we use it here. It's not too good. Children's shoes can be consulted: Cocos2d-x 3.0 Development (14) using Uiscrollview to achieve the size of different items drag display

note here that scrollview the child control layout, we select Linear Portrait, and the slide direction with properties is also selected as "vertical". Save the export after editing it well.

3. Related Programs

Run the script, create a testlayout program, and copy the exported files to the resource directory.

Declare a uilayout:

[CPP]View Plaincopyprint?
    1. #include "gui\cocosgui.h"
    2. Private
    3. gui::uilayout* m_layout;


Change init:

[CPP]View Plaincopyprint?
  1. BOOL Helloworld::init ()
  2. {
  3. //////////////////////////////  
  4. //1. Super init First
  5. if (! Layer::init ())
  6. {
  7. return false;
  8. }
  9. Size visiblesize = director::getinstance ()->getvisiblesize ();
  10. Point origin = Director::getinstance ()->getvisibleorigin ();
  11. //load Layout
  12. M_layout = dynamic_cast<uilayout*> (Cocostudio::guireader::sharereader ()->widgetfromjsonfile ("  Testlayout.json "));
  13. Auto layer = Uilayer::create ();
  14. Layer->addwidget (m_layout);
  15. this->addchild (layer);
  16. //set button React
  17. uibutton* Buttona = dynamic_cast<uibutton*> (m_layout->getchildbyname ("Textbuttona"));
  18. Buttona->addtoucheventlistener (this,toucheventselector (Helloworld::touchbutton));
  19. uibutton* buttonb = dynamic_cast<uibutton*> (m_layout->getchildbyname ("textbuttonb"));
  20. Buttonb->addtoucheventlistener (this,toucheventselector (Helloworld::touchbutton));
  21. return true;
  22. }


Add a response function


[CPP]View Plaincopyprint?
  1. void Helloworld::touchbutton (cocos2d::object* obj,toucheventtype type)
  2. {
  3. if (type = = touch_event_ended)
  4. {
  5. Auto button = dynamic_cast<uibutton*> (obj);
  6. if (strcmp (Button->getname (),"Textbuttona") = = 0)
  7. {
  8. //add element on left
  9. Addleft ();
  10. }
  11. Else
  12. {
  13. //add element on right
  14. Addright ();
  15. }
  16. }
  17. }


Compile and run, you can see the following interface:


4. Adding controls

Implement Addleft () and Addright ():

[CPP]View Plaincopyprint?
  1. void Helloworld::addleft ()
  2. {
  3. Auto Layoutparameter = Uilinearlayoutparameter::create ();
  4. Layoutparameter->setgravity (Uilineargravity::linear_gravity_left);
  5. Auto ScrollView = ((uiscrollview*) m_layout->getchildbyname ("ScrollView"));
  6. uiwidget* widget = Makewords ();
  7. Widget->setlayoutparameter (Layoutparameter);
  8. Scrollview->addchild (widgets);
  9. Scrollview->dolayout ();
  10. }
  11. void Helloworld::addright ()
  12. {
  13. Auto Layoutparameter = Uilinearlayoutparameter::create ();
  14. Layoutparameter->setgravity (Uilineargravity::linear_gravity_right);
  15. Auto ScrollView = ((uiscrollview*) m_layout->getchildbyname ("ScrollView"));
  16. uiwidget* widget = Makewords ();
  17. Widget->setlayoutparameter (Layoutparameter);
  18. Widget->setcolor (color3b (0,255,0));
  19. Scrollview->addchild (widgets);
  20. Scrollview->dolayout ();
  21. }
  22. gui::uiwidget* Helloworld::makewords ()
  23. {
  24. uibutton* button = Uibutton::create ();
  25. Button->loadtexturenormal ("button.png");
  26. Button->settitletext ("Hello uilayout \ n I ' m cocos2d-x");
  27. Button->settitlecolor (color3b (0,0,0));
  28. return button;
  29. }


Use the overridden Addchild to add child controls. Controls the display position of the control by adjusting its uilinearlayoutparameter.

One thing to note is that when you add a child control in Uiscrollview, you call the Dolayout function to organize the location of the child controls.

The final compilation runs.

5. Summary

with Uilayout layout, you can easily control the location of dynamically added elements.

Demo Download: http://download.csdn.net/detail/fansongy/6730481


This blog from the Asura road , reproduced please indicate the source, prohibited for commercial use: http://blog.csdn.net/fansongy/article/details/17382049

Internship Small white:: (EXT) Cocos2d-x 3.0 Development (15) Use Uilayout Layout, make dialog interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.