标签:
触摸的使用:
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listenerKeyPad = EventListenerKeyboard::create();
listenerKeyPad->onKeyPressed = [](EventKeyboard::KeyCodekeycode,Event* event){
CCLOG("onKeyPressed");
};
listenerKeyPad->onKeyReleased = [=](EventKeyboard::KeyCodekeycode,Event* event){
if (keycode == EventKeyboard::KeyCode::KEY_BACKSPACE) {
CCLOG("OK,You successful!");
}
KEY_DPAD_LEFT
KEY_DPAD_RIGHT
KEY_DPAD_UP
KEY_DPAD_DOWN
CCLOG("onKeyReleased%d",keycode);
char hh[10] = {};
sprintf(hh, "%d",keycode);
label->setString(hh);
};
将事件添加到事件调度中,设定优先级
dispatcher->addEventListenerWithFixedPriority(listenerKeyPad, 2);
将当前事件添加到事件调度中
dispatcher->addEventListenerWithSceneGraphPriority(listenerKeyPad, this);
聚焦事件:
//register focus event
_eventListener = EventListenerFocus::create();
_eventListener->onFocusChanged =CC_CALLBACK_2(HelloWorld::onFocusChanged, this);
_eventDispatcher->addEventListenerWithFixedPriority(_eventListener, 1);
void HelloWorld::onFocusChanged(cocos2d::ui::Widget*widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus)
{
CCLOG("onFocusChanged");
Layout *getLayout = dynamic_cast<Layout*>(widgetGetFocus);
if (!getLayout && widgetGetFocus) {
widgetGetFocus->setScale(1.2);
widgetGetFocus->setColor(Color3B::WHITE);
tag = widgetGetFocus->getTag();
}
Layout *loseLayout = dynamic_cast<Layout*>(widgetLostFocus);
if (!loseLayout && widgetLostFocus) {
widgetLostFocus->setScale(1.0);
widgetLostFocus->setColor(Color3B::WHITE);
}
}
键盘事件:
//register the keyboard event
_keyboardListener = EventListenerKeyboard::create();
_keyboardListener->onKeyReleased =CC_CALLBACK_2(HelloWorld::onKeyboardReleased, this);
_eventDispatcher->addEventListenerWithFixedPriority(_keyboardListener, 2);
this->setTouchMode(Touch::DispatchMode::ALL_AT_ONCE);
int count = 3;
for (int i = 0; i < count; ++i) {
ImageView *w = ImageView::create("CloseNormal.png");
w->setTouchEnabled(true);
w->setTag(i);
w->setLayoutParameter(vparams);
w->addTouchEventListener(CC_CALLBACK_2(HelloWorld::onImageViewClicked,this));
_horizontalLayout->addChild(w);
if(i == 0){
w->requestFocus();
}
}
void HelloWorld::onKeyboardReleased(EventKeyboard::KeyCodekeyCode, Event* e)
{
Label *label = (Label*)this->getChildByTag(600);
if (label) {
char mm[10] = {};
sprintf(mm, "%d",keyCode);
label->setString(mm);
}
if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_DOWN) {
MessageBox("down", "pressed");
}
else if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_UP) {
MessageBox("up", "pressed");
}
if (keyCode == (EventKeyboard::KeyCode)28) {
//模拟android中的按键
cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_UP;
cocos2d::EventKeyboard event(cocos2dKey, false);
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}else if (keyCode == (EventKeyboard::KeyCode)29){
cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_DOWN;
cocos2d::EventKeyboard event(cocos2dKey, false);
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}else if(keyCode == (EventKeyboard::KeyCode)26){
cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_LEFT;
cocos2d::EventKeyboard event(cocos2dKey, false);
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}else if(keyCode == (EventKeyboard::KeyCode)27){
cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_RIGHT;
cocos2d::EventKeyboard event(cocos2dKey, false);
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}else if(keyCode == (EventKeyboard::KeyCode)35){
CCLOG(">>>>>>>tag=%d",tag);
}
}
//图片被选中时的回调:
void HelloWorld::onImageViewClicked(cocos2d::Ref *ref, Widget::TouchEventType touchType)
{
if (touchType == Widget::TouchEventType::ENDED) {
Widget *w = (Widget*)ref;
if (w->isFocusEnabled()) {
w->setFocusEnabled(false);
w->setColor(Color3B::YELLOW);
}else{
w->setFocusEnabled(true);
w->setColor(Color3B::WHITE);
}
}
}
标签:
原文地址:http://www.cnblogs.com/yydblog/p/5422208.html