OnTouchMove 事件未触发
OnTouchMove event not triggered
我正在尝试在一个简单的场景中拖放精灵。 onTouchBegan 和 onTouchEnded 事件正常触发,但 onTouchMove 事件没有触发,为什么?
这是我的代码:
class Test: public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(Test);
bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event);
void onTouchMove(cocos2d::Touch *touch, cocos2d::Event *event);
void onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event);
cocos2d::Sprite *catSprite;
cocos2d::Vec2 touchPosition;
};
对于 Cpp:
Scene* Test::createScene()
{
auto scene = Scene::create();
auto layer = Test::create();
scene->addChild(layer);
return scene;
}
bool Test::init()
{
if ( !Layer::init() )
{
return false;
}
auto origin = Director::getInstance()->getVisibleOrigin();
auto windowSize = Director::getInstance()->getVisibleSize();
auto background = DrawNode::create();
background->drawSolidRect(origin, windowSize, Color4F(0.6, 0.6, 0.6, 1.0));
this->addChild(background, -1);
auto releaseArea = DrawNode::create();
releaseArea->drawSolidRect(windowSize*0.8,windowSize, Color4F(1.0,0.0,0.0,1.0));
this->addChild(releaseArea);
auto pinfo = AutoPolygon::generatePolygon("cat.png");
catSprite = Sprite::create(pinfo);
catSprite->setPosition(Vec2(windowSize.width*0.1, windowSize.height*0.1));
this->addChild(catSprite,1);
touchPosition.set(0,0);
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(Test::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(Test::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(Test::onTouchEnded, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
bool Test::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {
cocos2d::log("touch began");
touchPosition = touch->getLocation();
if(catSprite->getBoundingBox().containsPoint(touchPosition))
{
return true;
}
return false;
}
void Test::onTouchMove(cocos2d::Touch *touch, cocos2d::Event *event) {
cocos2d::log("touch moved");
catSprite->setPosition(catSprite->getPosition() + touch->getDelta());
}
void Test::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event) {
cocos2d::log("touch ended");
}
我从 cocos2d::log 那里得到了关于开始和结束事件的正确反馈,但是当我测试它时,移动事件似乎从未发生过。
调用的事件是 onTouchMoved 而不是 onTouchMove。
问候。
我自己。
我正在尝试在一个简单的场景中拖放精灵。 onTouchBegan 和 onTouchEnded 事件正常触发,但 onTouchMove 事件没有触发,为什么?
这是我的代码:
class Test: public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(Test);
bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event);
void onTouchMove(cocos2d::Touch *touch, cocos2d::Event *event);
void onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event);
cocos2d::Sprite *catSprite;
cocos2d::Vec2 touchPosition;
};
对于 Cpp:
Scene* Test::createScene()
{
auto scene = Scene::create();
auto layer = Test::create();
scene->addChild(layer);
return scene;
}
bool Test::init()
{
if ( !Layer::init() )
{
return false;
}
auto origin = Director::getInstance()->getVisibleOrigin();
auto windowSize = Director::getInstance()->getVisibleSize();
auto background = DrawNode::create();
background->drawSolidRect(origin, windowSize, Color4F(0.6, 0.6, 0.6, 1.0));
this->addChild(background, -1);
auto releaseArea = DrawNode::create();
releaseArea->drawSolidRect(windowSize*0.8,windowSize, Color4F(1.0,0.0,0.0,1.0));
this->addChild(releaseArea);
auto pinfo = AutoPolygon::generatePolygon("cat.png");
catSprite = Sprite::create(pinfo);
catSprite->setPosition(Vec2(windowSize.width*0.1, windowSize.height*0.1));
this->addChild(catSprite,1);
touchPosition.set(0,0);
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(Test::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(Test::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(Test::onTouchEnded, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
bool Test::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {
cocos2d::log("touch began");
touchPosition = touch->getLocation();
if(catSprite->getBoundingBox().containsPoint(touchPosition))
{
return true;
}
return false;
}
void Test::onTouchMove(cocos2d::Touch *touch, cocos2d::Event *event) {
cocos2d::log("touch moved");
catSprite->setPosition(catSprite->getPosition() + touch->getDelta());
}
void Test::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event) {
cocos2d::log("touch ended");
}
我从 cocos2d::log 那里得到了关于开始和结束事件的正确反馈,但是当我测试它时,移动事件似乎从未发生过。
调用的事件是 onTouchMoved 而不是 onTouchMove。
问候。 我自己。