Cocos2d-x常用功能-Cocos2d-x用户交互事件处理:触摸、传感器、物理按键

前端之家收集整理的这篇文章主要介绍了Cocos2d-x常用功能-Cocos2d-x用户交互事件处理:触摸、传感器、物理按键前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
第三阶段:常用功能3-
--Cocos2d-x常用功能-Cocos2d-x用户交互事件处理:触摸、传感器、物理按键(共6部分)
1.Cocos2d-x单点触摸
Size size = Director::getInstance()->getVisibleSize();


auto label = LabelTTF::create( "Click me" , "Courier" , 30 );
label->setPosition(size.width/
2 ,size.height/ 2 );
addChild(label);

auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch *t,Event *e){

if (e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLocation())) {

log(
"onTouchBegan" );
}

return true ;
};

listener->onTouchMoved = [](Touch* t,Event *e){
log(
"onTouchMoved" );
};

listener->onTouchEnded = [](Touch *t,27)">"onTouchEnd"
);
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,label);
2.Cocos2d-x触摸目标判断
//获取当前事件目标也就是label
"onTouchBegan" );
}
3.Cocos2d-x事件传递
onTouchBegan事件 true ;之后才能执行 onTouchMoved、 onTouchEnded
listener->onTouchMoved = [](Touch* t,27)">"onTouchEnd" );
};
4.Cocos2d-x多点触摸
安卓的DNK首先配置在环境变量里面
proj.android--- build_native.py
Size visibleSize = Director::getInstance()->getVisibleSize();

auto listener = EventListenerTouchAllAtOnce::create();

listener->onTouchesBegan = [](std::vector<Touch*> ts,27)">"onTouchesBegan"
);
};

listener->onTouchesMoved = [](std::vector<Touch*> ts,27)">"touches moved,and touch count is %ld"
,ts.size());
};

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);

5.Cocos2d-x加速度
acceleration 加速度 加速 stamp 标志/ 邮票;印记 double类型 %g
JAVA ADT 里面 右键import 打开proj.android,然后删除依赖项
cocos2d ▸ cocos ▸ platform ▸ android ▸ java ▸ src 放在JAVA ADT 的 src 里面 按下F11 选中Android Aplication

//加速度传感器默认处于关闭状态,需要打开
Device::setAccelerometerEnabled( true );

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(EventListenerAcceleration::create([](Acceleration *a,Event *e){

log(
"x:%g,y:%g,z:%g" ,a->x,a->y,a->z);
}),162)"> this);

6.Cocos2d-x物理按键交互
auto listener = EventListenerKeyboard::create();
listener->onKeyReleased = [](EventKeyboard::KeyCode code,27)">"key code : %d"
,code);

switch (code) {
case EventKeyboard::KeyCode::KEY_BACKSPACE:
Director::getInstance()->end();
break ;

default :
break ;
}
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,162)">this);
原文链接:https://www.f2er.com/cocos2dx/345730.html

猜你在找的Cocos2d-x相关文章