单点触摸
//触摸开始
bool Chapter05::onTouchBegan(Touch*touch,Event *unused_event)
//{
// log("Touch Began\r\n");
// Vec2 touchPoint =touch->getLocation();
// log("x:%f,y%f",touchPoint.x,touchPoint.y);
// return true;
//}
//触摸移动
// void Chapter05::onTouchMoved(Touch*touch,Event *unused_event)
//{
// auto drawNode=DrawNode::create();//创造 轨迹
// this->addChild(drawNode,10);
// Vec2 touchPoint =touch->getPreviousLocation();
// log("Touch Moved\r\n");
// log("x:%f,y%f",touchPoint.x,touchPoint.y);
// if (sp->getBoundingBox().containsPoint(touch->getLocation()))//边界框 包含()
// {
// sp->setPosition(touch->getLocation());// 位置
drawNode->drawLine(touch->getLocation(), touch->getPreviousLocation(), Color4F(1, 0, 0, 1));
// }
// drawNode->drawLine(touch->getLocation(), touch->getPreviousLocation(), Color4F(1, 0, 0, 1));
//
//}
触摸结束
// void Chapter05::onTouchEnded(Touch*touch,Event *unused_event)
//{
// Vec2 touchPoint =touch->getStartLocation();
// log("Touch Ended\r\n");
//
//}
触摸取消:例 手机出现了来电,是我们游戏进入后台
// void Chapter05::onTouchCancelled(Touch*touch,Event *unused_event)
//{
// long("Touch Cancelled\r\n");
//}
//单点触摸
onEnter()
{
/*// auto touchListener=EventListenerTouchOneByOne::create();//触摸监听
// touchListener->onTouchBegan=CC_CALLBACK_2(Chapter05::onTouchBegan, this);
// touchListener->onTouchMoved=CC_CALLBACK_2(Chapter05::onTouchMoved, this);
// touchListener->onTouchEnded=CC_CALLBACK_2(Chapter05::onTouchEnded, this);
// touchListener->onTouchCancelled=CC_CALLBACK_2(Chapter05::onTouchCancelled, this);
//
// Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority (touchListener,this);//注册该监听
// drawNode->drawLine(touch->getLocation(), touch->getPreviousLocation(), Color4F(1, 0, 0, 1));*/
}
在哪里注册就在哪里销毁,销毁触摸接听
void Chapter05::onExit()
{
Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);
Layer::onExit();
}
或者种形式
void ChaCase1::onEnter()
{
Layer::onEnter();
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(ChaCase1::onTouchBegan,this);
touchListener->onTouchMoved = CC_CALLBACK_2(ChaCase1::onTouchMoved,this);
touchListener->onTouchEnded = CC_CALLBACK_2(ChaCase1::onTouchEnded, this);
touchListener->onTouchCancelled=[=](Touch* t,Event * e)
{
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
};
touchListener->setSwallowTouches(true);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener,