第三阶段:常用功能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,Event *e){
log( "onTouchEnd" );
};
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,Event *e){
log( "onTouchEnd" );
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, label);
2.Cocos2d-x触摸目标判断
//获取当前事件目标也就是label
if
(e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLocation())) {
log( "onTouchBegan" );
}
log( "onTouchBegan" );
}
3.Cocos2d-x事件传递
onTouchBegan事件
return
true
;之后才能执行
onTouchMoved、
onTouchEnded
listener->onTouchMoved = [](Touch* t,Event *e){
log( "onTouchMoved" );
};
listener->onTouchEnded = [](Touch *t,Event *e){
log( "onTouchEnd" );
};
log( "onTouchMoved" );
};
listener->onTouchEnded = [](Touch *t,Event *e){
log( "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,Event *e){
log( "onTouchesBegan" );
};
listener->onTouchesMoved = [](std::vector<Touch*> ts,Event *e){
log( "touches moved ,and touch count is %ld" ,ts.size());
};
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = [](std::vector<Touch*> ts,Event *e){
log( "onTouchesBegan" );
};
listener->onTouchesMoved = [](std::vector<Touch*> ts,Event *e){
log( "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);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(EventListenerAcceleration::create([](Acceleration *a,Event *e){
log( "x:%g,y:%g,z:%g" ,a->x,a->y,a->z);
}), this);
6.Cocos2d-x物理按键交互
auto
listener = EventListenerKeyboard::create();
listener->onKeyReleased = [](EventKeyboard::KeyCode code,Event *e){
log( "key code : %d" ,code);
switch (code) {
case EventKeyboard::KeyCode::KEY_BACKSPACE:
Director::getInstance()->end();
break ;
default :
break ;
}
};
listener->onKeyReleased = [](EventKeyboard::KeyCode code,Event *e){
log( "key code : %d" ,code);
switch (code) {
case EventKeyboard::KeyCode::KEY_BACKSPACE:
Director::getInstance()->end();
break ;
default :
break ;
}
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);