我正在编写一个cocos2d-x 3.71跨平台应用程序(ios,android,windows10),并且我在触控和保持检测方面遇到了问题仅适用于Android (ios和窗户工作得很好)。我已经跟踪了这个原因,即当按下触摸而不是移动时, onTouchMoved ()会不断发射。我已经整合了一个解决方案,但这似乎是一个错误,我想知道是否有人有任何想法。
我在基于cocos2d-x HelloWorld示例的最小应用程序中重新创建了该问题。我只是从 cocos2d :: Node (testNode)派生了一个类并创建它并将其作为子项添加到HelloWorldScene中。在testNode中,我覆盖了更新函数,并使用 onTouchBegan ()中的 scheduleUpdate ()进行计划。在update()函数中,我只计算时间直到达到0.25秒,然后使用 unscheduleUpdate ()禁用更新。我在 onTouchMoved ()中进行了另一次 unscheduleUpdate ()调用,以便在触摸移动时停止计算保持时间。问题是,在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调试信息(32508): startDistFromCenter:33.753014 09-23 23:42:39.902:D / cocos2d-x debug info(32508):moving ... pos = 246.732849,126.925316 09-23 23:42:39.902:D / cocos2d-x调试信息(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-23 23:42:39.969:D / cocos2d-x调试信息(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-23 23:42:39.999:D / cocos2d-x调试信息(32508):移动... pos = 246.732849 ,126.925316 09-23 23:42:40.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-23 23:42:40.043:D / cocos2d-x调试信息(32508):保持时间:0.042597
正如你所看到的,位置不实际上正在移动....什么给出了?
供参考,我正在开发Windows 10,Visual Studio 2015,cocos2d-x 3.71,以及在Android 5.1.1(OnePlusOne手机)上进行测试
答案 0 :(得分:1)
我测试了你的代码,问题与android lollipop有关。在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页面上开始一个问题。