issue #2087: [dispatcher] Fix box2d_test crash.

This commit is contained in:
James Chen 2013-09-16 23:35:16 +08:00
parent 2d0ca10bbf
commit 365419a8b2
3 changed files with 18 additions and 11 deletions

View File

@ -98,8 +98,13 @@ Layer *Layer::create()
/// Touch and Accelerometer related
void Layer::onRegisterTouchListener()
{
void Layer::addTouchListener()
{
if (_touchListener != nullptr)
return;
auto dispatcher = EventDispatcher::getInstance();
if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE )
{
// Register Touch Event
@ -110,7 +115,7 @@ void Layer::onRegisterTouchListener()
listener->onTouchesEnded = CC_CALLBACK_2(Layer::onTouchesEnded, this);
listener->onTouchesCancelled = CC_CALLBACK_2(Layer::onTouchesCancelled, this);
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
_touchListener = listener;
}
else
@ -124,7 +129,7 @@ void Layer::onRegisterTouchListener()
listener->onTouchEnded = CC_CALLBACK_2(Layer::onTouchEnded, this);
listener->onTouchCancelled = CC_CALLBACK_2(Layer::onTouchCancelled, this);
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
_touchListener = listener;
}
}
@ -170,7 +175,7 @@ void Layer::setTouchEnabled(bool enabled)
{
if (enabled)
{
this->onRegisterTouchListener();
this->addTouchListener();
}
else
{
@ -309,7 +314,7 @@ void Layer::onEnter()
// since events are propagated in reverse order
if (_touchEnabled)
{
this->onRegisterTouchListener();
this->addTouchListener();
}
// then iterate over all the children
@ -348,6 +353,7 @@ void Layer::onEnterTransitionDidFinish()
if (_accelerometerEnabled)
{
auto dispatcher = EventDispatcher::getInstance();
dispatcher->removeEventListener(_accelerationListener);
_accelerationListener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this));
dispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this);
}

View File

@ -113,7 +113,6 @@ public:
@since v0.8.0
*/
CC_DEPRECATED_ATTRIBUTE virtual void registerWithTouchDispatcher() final {};
virtual void onRegisterTouchListener();
/** whether or not it will receive Touch events.
You can enable / disable touch events with this property.
@ -180,6 +179,8 @@ public:
virtual void onEnterTransitionDidFinish() override;
protected:
void addTouchListener();
bool _touchEnabled;
bool _accelerometerEnabled;
bool _keyboardEnabled;

View File

@ -53,6 +53,7 @@ bool MenuLayer::initWithEntryID(int entryId)
m_entryID = entryId;
setTouchEnabled( true );
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
Box2DView* view = Box2DView::viewWithEntryID( entryId );
addChild(view, 0, kTagBox2DNode);
@ -90,7 +91,7 @@ bool MenuLayer::initWithEntryID(int entryId)
listener->onTouchBegan = CC_CALLBACK_2(MenuLayer::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(MenuLayer::onTouchMoved, this);
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 0);
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 1);
_touchListener = listener;
return true;
}
@ -185,6 +186,7 @@ bool Box2DView::initWithEntryID(int entryId)
m_entry = g_testEntries + entryId;
m_test = m_entry->createFcn();
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
// Removes Touch Event Listener
EventDispatcher::getInstance()->removeEventListener(_touchListener);
@ -197,7 +199,7 @@ bool Box2DView::initWithEntryID(int entryId)
listener->onTouchMoved = CC_CALLBACK_2(Box2DView::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(Box2DView::onTouchEnded, this);
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 10);
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, -10);
_touchListener = listener;
return true;
@ -268,8 +270,6 @@ void Box2DView::onTouchEnded(Touch* touch, Event* event)
log("Box2DView::onTouchEnded, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);
m_test->MouseUp(b2Vec2(nodePosition.x,nodePosition.y));
// EventDispatcher::getInstance()->setPriorityWithFixedValue(_touchEventId, -1);
}
// void Box2DView::accelerometer(UIAccelerometer* accelerometer, Acceleration* acceleration)