1
2
3
4
5
6
7
8
| // static EventListenerMouse* create(); std::function< void (Event* event)> onMouseDown; //按下鼠標, 單擊鼠標 std::function< void (Event* event)> onMouseUp; //鬆開鼠標, 按下的狀態下鬆開 std::function< void (Event* event)> onMouseMove; //移動鼠標, 在屏幕中移動 std::function< void (Event* event)> onMouseScroll; //滾動鼠標, 滾動鼠標的滾輪 // |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| // //獲取事件分發器 auto dispatcher = Director::getInstance()->getEventDispatcher(); //創建鼠標事件監聽器 EventListenerMouse EventListenerMouse* mouseListenter = EventListenerMouse::create(); //鼠標事件響應函數 mouseListenter->onMouseDown = CC_CALLBACK_1(HelloWorld::onMouseDown, this ); mouseListenter->onMouseUp = CC_CALLBACK_1(HelloWorld::onMouseUp, this ); mouseListenter->onMouseMove = CC_CALLBACK_1(HelloWorld::onMouseMove, this ); mouseListenter->onMouseScroll = CC_CALLBACK_1(HelloWorld::onMouseScroll, this ); //添加鼠標事件監聽器,事件響應處理委託給this dispatcher->addEventListenerWithSceneGraphPriority(mouseListenter, this ); //事件響應函數 void onMouseDown(Event* event) { CCLOG( "Down" ); } void onMouseUp(Event* event) { CCLOG( "UP" ); } void onMouseMove(Event* event) { CCLOG( "MOVE" ); } void onMouseScroll(Event* event) { CCLOG( "Scroll" ); } // |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| // static EventListenerKeyboard* create(); std::function< void (EventKeyboard::KeyCode, Event*)> onKeyPressed; //按下某鍵 std::function< void (EventKeyboard::KeyCode, Event*)> onKeyReleased; //鬆開某鍵 //鍵盤按鍵枚舉類型 EventKeyboard::KeyCode //KeyCode的值對應的不是鍵盤的鍵值、也不是ASCII碼,只是純粹的枚舉類型 //如: // EventKeyboard::KeyCode::KEY_A // EventKeyboard::KeyCode::KEY_1 // EventKeyboard::KeyCode::KEY_F1 // EventKeyboard::KeyCode::KEY_SPACE // EventKeyboard::KeyCode::KEY_ALT // EventKeyboard::KeyCode::KEY_SHIFT // |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| // //獲取事件分發器 auto dispatcher = Director::getInstance()->getEventDispatcher(); //創建鍵盤按鍵事件監聽器 EventListenerKeyboard* keyboardListener = EventListenerKeyboard::create(); //綁定事件響應函數 keyboardListener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed, this ); keyboardListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this ); //添加監聽器 dispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this ); //事件響應函數 void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) { if (EventKeyboard::KeyCode::KEY_J == keyCode) { CCLOG( "Pressed: J" ); } } void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) { if (EventKeyboard::KeyCode::KEY_SPACE == keyCode) { CCLOG( "Released: SPACE" ); } } // |
1
2
3
4
5
6
| //加速計信息 class Acceleration { double x; double y; double z; }; // |
1
2
3
4
5
| // static EventListenerAcceleration* create( const std::function< void (Acceleration*, Event*)>& callback); std::function< void (Acceleration*, Event*)> onAccelerationEvent; // |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| // //標籤: 顯示加速計信息 label = Label::createWithTTF( "no used" , "Marker Felt.ttf" , 12); label->setPosition(visibleSize / 2); this ->addChild(label); //小球: 可視化加速計 ball = Sprite::create( "ball.png" ); ball->setPosition(visibleSize / 2); this ->addChild(ball); //獲取事件分發器 auto dispatcher = Director::getInstance()->getEventDispatcher(); //需要開啟移動設備的加速計 Device::setAccelerometerEnabled( true ); //創建加速計事件監聽器 auto accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(HelloWorld::onAccelerationEvent, this )); //添加加速計監聽器 dispatcher->addEventListenerWithSceneGraphPriority(accelerationListener, this ); //事件響應函數 void HelloWorld::onAccelerationEvent(Acceleration* acceleration, Event* event) { char s[100]; sprintf (s, "X: %f; Y: %f; Z:%f; " , acceleration->x, acceleration->y, acceleration->z); label->setString(s); //改變小球ball的位置 float x = ball->getPositionX() + acceleration->x * 10; float y = ball->getPositionY() + acceleration->y * 10; Vec2 pos = Vec2(x, y); pos.clamp(ball->getContentSize() / 2, Vec2(288, 512) - ball->getContentSize() / 2); ball->setPosition(pos); //設置位置 } // |
1
2
3
4
5
| // //eventName : 監聽器名字,即消息的名稱 //callback : 監聽器函數,即消息的回調函數 static EventListenerCustom* create( const std::string& eventName, const std::function< void (EventCustom*)>& callback); // |
1
2
3
4
5
| // EventCustom event( "custom_event" ); event->setUserData(( void *)123); // 綁定消息傳遞的數據,可以為任意類型void。 dispatcher->dispatchEvent(&event); // 發佈名稱為"custom_event"的消息。 // |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| // //獲取事件分發器 auto dispatcher = Director::getInstance()->getEventDispatcher(); //創建自定義事件監聽器 //監聽器名字 : "custom_event" //事件響應函數: HelloWorld::onCustomEvent auto customListener = EventListenerCustom::create( "custom_event" , CC_CALLBACK_1(HelloWorld::onCustomEvent, this )); //添加自定義事件監聽器,優先權為1 dispatcher->addEventListenerWithFixedPriority(customListener, 1); //手動分發監聽器的事件,通過dispatchEvent發佈名稱為custom_event的消息。 EventCustom event = EventCustom( "custom_event" ); event->setUserData(( void *)123); // 綁定消息傳遞的數據,可以為任意類型void。 dispatcher->dispatchEvent(&event); //消息事件回調函數 void HelloWorld::onCustomEvent(EventCustom* event) { // 獲取消息傳遞的數據 int * data = ( int *)event->getUserData() CCLOG( "onCustomEvent data = %d" , data); } // |
1
2
3
4
5
6
| // EventListenerPhysicsContact; EventListenerPhysicsContactWithBodies; EventListenerPhysicsContactWithGroup; EventListenerPhysicsContactWithShapes; // |
1
2
3
| // EventListenerController; //
|
沒有留言:
張貼留言