mirror of https://github.com/axmolengine/axmol.git
Merge pull request #3635 from dumganhar/box2d_test_bed_fix
issue #2087: [dispatcher] Fix box2d_test crash.
This commit is contained in:
commit
43e97f240a
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -573,19 +573,16 @@ RemoveMenuItemWhenMove::RemoveMenuItemWhenMove()
|
|||
|
||||
setTouchEnabled(true);
|
||||
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
|
||||
}
|
||||
|
||||
void RemoveMenuItemWhenMove::onRegisterTouchListener()
|
||||
{
|
||||
|
||||
// Register Touch Event
|
||||
auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
listener->setSwallowTouches(false);
|
||||
_touchListener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
_touchListener->setSwallowTouches(false);
|
||||
|
||||
listener->onTouchBegan = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchBegan, this);
|
||||
listener->onTouchMoved = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchMoved, this);
|
||||
_touchListener->onTouchBegan = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchBegan, this);
|
||||
_touchListener->onTouchMoved = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchMoved, this);
|
||||
|
||||
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(_touchListener, -100);
|
||||
|
||||
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, -100);
|
||||
_touchListener = listener;
|
||||
}
|
||||
|
||||
void RemoveMenuItemWhenMove::goBack(Object *pSender)
|
||||
|
|
|
@ -107,8 +107,6 @@ public:
|
|||
RemoveMenuItemWhenMove();
|
||||
~RemoveMenuItemWhenMove();
|
||||
|
||||
virtual void onRegisterTouchListener() override;
|
||||
// virtual void registerWithTouchDispatcher(void);
|
||||
virtual bool onTouchBegan(Touch *touch, Event *event);
|
||||
virtual void onTouchMoved(Touch *touch, Event *event);
|
||||
|
||||
|
|
Loading…
Reference in New Issue