cococos 2 d-xプロジェクト101回の出会い:3.0のクリックイベント、CCTouchDelegateはすでに停止しました
cococos 2 d−x 3.0は、touchイベントをspriteにバンドルするためにTouchDelegate方式を使用しなくなった.
新しい方法は
auto listener =EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(CMyFirstScene::onTouchBegan,this);
listener->onTouchMoved = CC_CALLBACK_2(CMyFirstScene::onTouchMoved,this);
listener->onTouchEnded = CC_CALLBACK_2(CMyFirstScene::onTouchEnded,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
这段代码可以放在.cpp里面的 init里。
头文件增加:
Sprite*s;
//
bool onTouchBegan(Touch* touch,Event*event);
void onTouchMoved(Touch* touch,Event*event);
void onTouchEnded(Touch* touch,Event*event);
void selectSpriteForTouch(Point touchLocation);
s 为全局的精灵。
boolCMyFirstScene::onTouchBegan(Touch* touch,Event*event)
{
CCLOG("TouchBegan");
Point touchLocation =this->convertTouchToNodeSpace(touch);
this->selectSpriteForTouch(touchLocation);
returntrue;
}
voidCMyFirstScene::onTouchMoved(Touch* touch,Event*event)
{
CCLOG("TouchMoved");
}
voidCMyFirstScene::onTouchEnded(Touch* touch,Event*event)
{
CCLOG("TouchEnded");
}
voidCMyFirstScene::selectSpriteForTouch(Point touchLocation)
{
if(s->getBoundingBox().containsPoint(touchLocation))
{
Action* actionMove =
MoveTo::create(2.0,
ccp(300,200));
s->runAction(actionMove);
}
}
, , 。