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