COCOS2DX--Learning notes making virtual keys with UIButton

Source: Internet
Author: User

Today, continue to improve their own small demo, to join some virtual keys, first of all, up and down the direction of the key.

This needs to be achieved, press the continuous walk, release the effect of the stop.

Try to do with the ccmenuimage, unfortunately Ccmenuimage only support the press and reload, after the event processing. It is not possible to control the time between pressing and lifting.

UIButton just can meet this demand.

UIButton is a UI control class in the cocos2dx extension, derived from the widget.

Using a method similar to Ccmenuimage, each UI control must be placed in a uilayer, that is, Uilayer is the container for the UI control.


Create the words similar to the following:

Button *m_pupbtn = Button::create (); m_pupbtn->settouchenabled (true); M_pupbtn->loadtextures ("DirKeyNor.png", "Dirkeysel.png", "" "); M_pupbtn->setposition (CCP (180.0f, 160.0f + m_pupbtn->getcontentsize (). height/2)); m_ Pupbtn->addtoucheventlistener (This, Toucheventselector (Dirpanel::ondirup)); Uilayer->addwidget (M_PUPBTN);
Incidentally, in fact, UIButton this class does not exist.
typedef Cocos2d::ui::button UIButton;

It's actually derived from a TypeDef, and the real control class is a button.


With the button, we just need to create four button controls and then add them to Uilayer, of course, Uilayer may be added to the node. Then add a touch response event for each button.

The form resembles this:

void Dirpanel::ondirup (Cocos2d::ccobject *obj, Cocos2d::ui::toucheventtype type) {switch (type) {case Touch_event_ Began:{m_phero->setdir (DIR_UP);} Break;case Touch_event_ended:{m_phero->setdir (dir_none);} Break;}}


With these, the virtual keys are extremely easy, of course, in order to match the virtual keys need to control the movement of the interface of the characters.

At this point, the virtual keys will be landed.



COCOS2DX--Learning notes making virtual keys with UIButton

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.