Cocos2d-x onTouchMoved在没有移动发生时调用
Cocos2d-x onTouchMoved called when no movement is occurring
我正在编写一个cocos2d-x 3.71跨平台应用(ios, android, windows10),我在android上执行触摸和按住检测时遇到了麻烦(ios和windows工作得很好)。我已经追踪到这一点的原因归结为这样一个事实,即当按住触摸而不移动时,onTouchMoved()不断发射。我已经合并了一个解决方案,但这似乎是一个错误,我想知道是否有人有任何想法。
我在一个基于cocos2d-x HelloWorld示例的最小应用程序中重现了这个问题。我只是从cocos2d::Node (testNode)派生了一个类,并创建了它,并将其作为子类添加到HelloWorldScene中。在testNode中,我覆盖了更新函数并使用scheduleUpdate()从onTouchBegan()调度它。在update()函数中,我只是计算时间,直到达到0.25秒,然后使用unscheduleUpdate()禁用更新。我有另一个unscheduleUpdate()调用onTouchMoved()停止计数保持时间,以防触摸移动。问题是,在Android上,当触摸静止不动时,onTouchMoved()开始不断触发。testNode.h:
#pragma once
#include "cocos2d.h"
class testNode : public cocos2d::Node
{
public:
testNode() {}
~testNode() {}
CREATE_FUNC(testNode);
virtual bool init();
virtual void update(float dt);
bool onTouchBegan(cocos2d::Touch*, cocos2d::Event*);
void onTouchMoved(cocos2d::Touch*, cocos2d::Event*);
void onTouchEnded(cocos2d::Touch*, cocos2d::Event*);
void onTouchCancelled(cocos2d::Touch*, cocos2d::Event*);
private:
cocos2d::DrawNode* d;
int id;
bool touchIsDown;
float touchTime;
cocos2d::Label *messageLabel;
cocos2d::Vec2 center;
};
testNode.cpp:
#include "testNode.h"
USING_NS_CC;
bool testNode::init()
{
if (!Node::init())
{
return false;
}
setContentSize(Size(50, 50));
Size size = Director::getInstance()->getWinSize();
Size visibleSize = Director::getInstance()->getVisibleSize();
setPosition(Vec2(size.width / 2.0f, size.height / 2.0f));
center.x = size.width / 2.0f;
center.y = size.height / 2.0f;
d = DrawNode::create();
addChild(d, 10);
d->drawCircle(Vec2(0, 0), 50, (float)M_PI * 2, 50, false, Color4F::RED);
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->setSwallowTouches(true);
touchListener->onTouchBegan = CC_CALLBACK_2(testNode::onTouchBegan, this);
touchListener->onTouchEnded = CC_CALLBACK_2(testNode::onTouchEnded, this);
touchListener->onTouchMoved = CC_CALLBACK_2(testNode::onTouchMoved, this);
touchListener->onTouchCancelled = CC_CALLBACK_2(testNode::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
messageLabel = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 18);
Vec2 origin = Director::getInstance()->getVisibleOrigin();
messageLabel->setPosition(Vec2(0,0));
addChild(messageLabel, 1);
return true;
}
void testNode::update(float deltaTime)
{
// When update is scheduled with scheduleUpdate(), this will increment the touchtimer
// used for differenciating different touch actions.
if (touchIsDown)
{
touchTime += deltaTime;
if (touchTime > 0.25)
{
unscheduleUpdate();
}
}
}
bool testNode::onTouchBegan(Touch* touch, Event* event)
{
Vec2 touchPoint = touch->getLocation();
float startDistFromCenter = center.getDistance(touchPoint);
touchTime = 0.0f;
// Handle touch only if inside the radius of the circle
if (startDistFromCenter <= 50)
{
// Track how long the touch is held without moving or releasing
touchIsDown = true;
scheduleUpdate();
return true;
}
else
return false;
}
void testNode::onTouchMoved(Touch* touch, Event* event)
{
Vec2 pos = touch->getLocation();
log("moving...pos = %f , %f", pos.x, pos.y);
touchIsDown = false;
unscheduleUpdate();
}
void testNode::onTouchEnded(Touch* touch, Event* event)
{
touchIsDown = false;
unscheduleUpdate();
log("Hold Time: %f", touchTime);
}
void testNode::onTouchCancelled(Touch* touch, Event* event)
{
}
触摸/按住事件的典型输出:
09-23 23:42:39.852: D/cocos2d-x debug info(32508):startDistFromCenter: 33.753014 09-23 23:42:39.902: D/cocos2d-x debug信息(32508):移动……Pos = 246.732849, 126.925316 09-2323:42:39.902: D/cocos2d-x debug info(32508): moving…Pos = 246.732849, 126.925316 09-23 23:42:39.920: D/cocos2d-x调试信息(32508):移动……pos = 246.732849, 126.925316 09-23 23:42:39.935: D/cocos2d-x调试信息(32508):moving…Pos = 246.732849, 126.925316 09-2323:42:39.969: D/cocos2d-x debug info(32508): moving…Pos = 246.732849, 126.925316 09-23 23:42:39.969: D/cocos2d-x调试信息(32508):移动……pos = 246.732849, 126.925316 09-23 23:42:39.989: D/cocos2d-x调试信息(32508):moving…Pos = 246.732849, 126.925316 09-2323:42:39.999: D/cocos2d-x debug info(32508): moving…Pos = 246.732849, 126.925316 09-23 23:42:40. 0.027: D/cocos2d-x调试信息(32508):移动……pos = 246.732849, 126.925316 09-23 23:42:40.043: D/cocos2d-x调试信息(32508):moving…Pos = 246.732849, 126.925316 09-2323:42:40.043: D/cocos2d-x debug info(32508): Hold Time: 0.042597
可以看到,位置是而不是实际移动....到底发生了什么事?
作为参考,我在Windows 10, Visual Studio 2015, cocos2d-x 3.71上进行开发,并在Android 5.1.1 (OnePlusOne手机)上进行测试
我测试了你的代码,问题与android棒棒糖有关。在kitkat上一切正常(测试设备为华为Ascend P7 android 4.4.2):
09-24 11:51:25.351: D/cocos2d-x debug info(1058): Hold Time: 0.252129
09-24 11:51:27.361: D/cocos2d-x debug info(1058): Hold Time: 0.251298
09-24 11:51:28.711: D/cocos2d-x debug info(1058): Hold Time: 0.252115
09-24 11:51:30.101: D/cocos2d-x debug info(1058): Hold Time: 0.251961
我也试过棒棒糖设备(LG,但我不记得确切的型号),就像你说的。触摸垃圾移动到相同的位置。
作为一种解决方法,你可以像Alex G说的那样,或者记住最后的位置(记住它在触摸开始时),忽略与位置完全相同的事件。
顺便说一句,我使用的是Mac OS X Yosemite和Cocos2d-x 3.8。
你应该在cocos2d-x github页面上启动一个问题
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- C++:为什么不调用移动构造函数?
- 为什么这里不调用移动构造函数?
- 为什么不调用移动构造函数
- 为什么调用移动分配?
- 当我将构造函数显式调用为参数时,不会调用移动构造函数
- 为什么只有在向量中已经有一个元素时才调用移动构造函数?
- 如果我们有 (N)RVO,当实际调用移动构造函数时?
- 与移动构造函数混淆:无法调用移动构造函数
- 当设置对象等于另一个函数的返回值时,为什么要调用移动构造函数/分配
- 尚未调用移动构造函数
- emplace_back调用移动构造函数和析构函数
- 为什么要调用移动构造函数
- 为什么在Lambda移动时未调用移动构造函数
- 我应该如何确保对移动构造函数的调用?(移动语义和右值引用)
- 在 c++ 中调用移动构造函数
- 从另一个函数的"if loop"中调用/移动一个函数 - Qt,C++
- 未调用移动赋值运算符
- 为什么不调用移动构造函数?
- 调用移动到Qt中不同线程的QObject函数?