cocos2d 手势识别
发布时间:2020-12-14 21:20:15 所属栏目:百科 来源:网络整理
导读:现在说一下cocos2d的手势识别的代码,其主要策略就是两点,记录接触点的起始位置和末尾位置,通过根据这两个点的位置计算来确定是左移、右移、下滑、点击四种手势 bool Teris::ccTouchBegan(CCTouch*touch,CCEvent*event) {if (containsTouchLocation(touch)
现在说一下cocos2d的手势识别的代码,其主要策略就是两点,记录接触点的起始位置和末尾位置,通过根据这两个点的位置计算来确定是左移、右移、下滑、点击四种手势 bool Teris::ccTouchBegan(CCTouch*touch,CCEvent*event) { if (containsTouchLocation(touch)) { CCPoint touchPoint = convertTouchToNodeSpace(touch); firstX = touchPoint.x; firstY = touchPoint.y; is_control = true; return true; } else { is_control = false; } } 这段代码主要是判断触摸点是否是在CCSprite的范围之内,如果在的话,则记录了触摸起始点,然后在ccTouchEnded函数中记录触摸结尾点,最后进行两者的算法计算 void Teris::ccTouchEnded(CCTouch*touch,CCEvent*event) { if (is_control) { CCPoint touchPoint = convertTouchToNodeSpace(touch); endX = firstX - touchPoint.x; endY = firstY - touchPoint.y; if (endX * endX + endY * endY < 2) { turn_(); return; } if (abs(endX) > abs(endY)) { if (endX + 5 > 0) { move_left(); } else { move_right(); } } else { if (endY + 5 > 0) { move_down(); } } } }这段代码的主要特点是首要计算起点和尾点的距离小于2则认为是点击事件,我们直接进行turn_()函数,其他的逻辑功能如代码所示了...哈哈 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |