标签:des style blog color os io strong div
点击事件检测
直接上代码
auto dispatcher = Director::getInstance()->getEventDispatcher(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this); dispatcher->addEventListenerWithSceneGraphPriority(listener, this); bool GameLayer::onTouchBegan(Touch* touch, Event *event) { return true; } void GameLayer::onTouchMoved(Touch* touch, Event *event) { } void GameLayer::onTouchEnded(Touch*touch, Event *event) { }
检测点击了Sprite
bool GameLayer::CheckTouchAli(Touch *touch) { //将点击点转换成自己坐标系中的坐标,即相对于aliSprite(一个Sprite)的原点 Vec2 pt = aliSprite->convertTouchToNodeSpace(touch); int nw = aliSprite->GetRect().size.width; int nh = aliSprite->GetRect().size.height; Rect rc(0,0, nw, nh); return rc.containsPoint(pt); }
Sprite跟随拖动
void GameLayer::SetAliPosToTouchPos(Touch* touch) { Vec2 pt = aliSprite->convertTouchToNodeSpace(touch); int nw = aliSprite->GetRect().size.width; int nh = aliSprite->GetRect().size.height; Vec2 touchPos = Vec2(aliSprite->getPositionX()-nw*0.5+pt.x ,aliSprite->getPositionY()-nh*0.5+pt.y); aliSprite->setPosition(touchPos); }
cocos2dx-3.2 笔记 - 点击事件,布布扣,bubuko.com
标签:des style blog color os io strong div
原文地址:http://www.cnblogs.com/kee-chen/p/3928837.html