add box2dbed test keyboard event.

This commit is contained in:
boyu0 2014-03-31 12:07:21 +08:00
parent 4fa8d24561
commit 9b75fc5595
2 changed files with 24 additions and 1 deletions

View File

@ -188,6 +188,12 @@ bool Box2DView::initWithEntryID(int entryId)
_eventDispatcher->addEventListenerWithFixedPriority(listener, -10);
_touchListener = listener;
auto keyboardListener = EventListenerKeyboard::create();
keyboardListener->onKeyPressed = CC_CALLBACK_2(Box2DView::onKeyPressed, this);
keyboardListener->onKeyReleased = CC_CALLBACK_2(Box2DView::onKeyReleased, this);
_eventDispatcher->addEventListenerWithFixedPriority(keyboardListener, -11);
_keyboardListener = keyboardListener;
return true;
}
@ -222,6 +228,7 @@ Box2DView::~Box2DView()
{
// Removes Touch Event Listener
_eventDispatcher->removeEventListener(_touchListener);
_eventDispatcher->removeEventListener(_keyboardListener);
delete m_test;
}
//
@ -239,7 +246,7 @@ bool Box2DView::onTouchBegan(Touch* touch, Event* event)
auto nodePosition = convertToNodeSpace( touchLocation );
log("Box2DView::onTouchBegan, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);
return m_test->MouseDown(b2Vec2(nodePosition.x,nodePosition.y));
return m_test->MouseDown(b2Vec2(nodePosition.x,nodePosition.y));
}
void Box2DView::onTouchMoved(Touch* touch, Event* event)
@ -262,6 +269,18 @@ void Box2DView::onTouchEnded(Touch* touch, Event* event)
m_test->MouseUp(b2Vec2(nodePosition.x,nodePosition.y));
}
void Box2DView::onKeyPressed(EventKeyboard::KeyCode code, Event* event)
{
log("Box2dView:onKeyPressed, keycode: %d", code);
m_test->Keyboard(static_cast<unsigned char>(code));
}
void Box2DView::onKeyReleased(EventKeyboard::KeyCode code, Event* event)
{
log("onKeyReleased, keycode: %d", code);
m_test->KeyboardUp(static_cast<unsigned char>(code));
}
// void Box2DView::accelerometer(UIAccelerometer* accelerometer, Acceleration* acceleration)
// {
// //// Only run for valid values

View File

@ -32,6 +32,7 @@ class Test;
class Box2DView : public Layer
{
EventListenerTouchOneByOne* _touchListener;
EventListenerKeyboard* _keyboardListener;
TestEntry* m_entry;
Test* m_test;
int m_entryID;
@ -47,6 +48,9 @@ public:
bool onTouchBegan(Touch* touch, Event* event);
void onTouchMoved(Touch* touch, Event* event);
void onTouchEnded(Touch* touch, Event* event);
void onKeyPressed(EventKeyboard::KeyCode code, Event* event);
void onKeyReleased(EventKeyboard::KeyCode code, Event* event);
//virtual void accelerometer(UIAccelerometer* accelerometer, Acceleration* acceleration);
static Box2DView* viewWithEntryID(int entryId);