所有人!我在一个场景中的组合,多点触摸和一次触摸有困难。所以让我一步一步地描述我的问题。
我使用cocos2dx(安卓),所以我有场景,在这个场景中我添加了层Pan Zoom Layer用于滚动和缩放。接下来,我在PanZoomLayer上添加了新的层(游戏层)。我需要捕捉点击游戏层(在这一层位于我的游戏的东西)我尝试这样做,使用模式观察者,我已经做了PanZoom层由“主题”我的游戏层-“观察者”,并在PanZoomLayer中创建枚举事件,并结构touchEvent
enum EVENTS{
EVENT_ONCE_CLICK,
EVENT_MOVE,
ANOTHER,
};
struct touchEvent {
EVENTS eventName;
};
并在类PanZoomLayer字段m_events中创建:
std::stack<EVENTS> m_events;
在onTouchesBegan上添加这样的代码
if (_touches.size() == 1) {
m_events.push(EVENTS::EVENT_ONCE_CLICK);
}
else {
m_events.push(EVENTS::ANOTHER);
}
在onTouchesMoved中,如果_touches.size() ==为1,则在分支中添加以下代码:
Vec2 curTouchPosition = Director::getInstance()->convertToGL(touch->getLocationInView());
Vec2 prevTouchPosition = Director::getInstance()->convertToGL(touch->getPreviousLocationInView());
Vec2 deltaPosition = curTouchPosition - prevTouchPosition;
float pos = curTouchPosition.distance(prevTouchPosition);
if (fabs(pos) < 0.5f) {
m_events.push(EVENTS::EVENT_ONCE_CLICK);
}
else {
m_events.push(EVENTS::ANOTHER);
}
一个onTouchesEnded,添加这样的代码:
if (m_events.empty())
return;
EVENTS ev = m_events.top();
while (!m_events.empty()) {
m_events.pop();
}
if (ev == EVENTS::EVENT_ONCE_CLICK) {
// MessageBox("once click", "title");
Touch *touch = (Touch *)_touches.at(0);
Vec2 position = touch->getLocation();
notifyClick(position);
}
其中notifyClick:
std::shared_ptr<CSceneSession> sessionShared = m_session.lock();
if (sessionShared)
sessionShared->clickOnScene(point);
所以我进入了游戏循环:游戏场景->菜单上的过渡场景->游戏场景......在开始时它做得很好,但在一段时间后(在不同设备上-不同时间)游戏开始奇怪的行为: buggy,
你能帮我个忙吗?感谢您的任何想法和建议。
发布于 2016-02-03 16:52:04
我通过以下方式解决了这个问题:在方法onEnter中删除scheduleUpdateForTarget,在方法onExit中删除unscheduleAllForTarget,并删除方法update。感谢您的关注!
https://stackoverflow.com/questions/34975006
复制相似问题