private:
bool onTouchBegan(Touch* tTouch,Event* eEvent)//手指按下事件
void onTouchMoved(Touch* tTouch,Event* eEvent)//手指移动事件
void onTouchEnded(Touch* tTouch,Event* eEvent)//手指离开事件
2.实现原型
bool ShopItem::onTouchBegan(Touch* tTouch,Event* eEvent){
if (sprite->getBoundingBox().containsPoint(tTouch->getLocation())){//判断触摸点是否在目标的范围内
/**这里为事件内容**/
return true
}else
return false
}
}
void ShopItem::onTouchMoved(Touch* tTouch,Event* eEvent){
/**这里为事件内容**/
}
void ShopItem::onTouchEnded(Touch* tTouch,Event* eEvent){
/**这里为事件内容**/
}
3.绑定事件
auto listener = EventListenerTouchOneByOne::create()
listener->onTouchBegan = CC_CALLBACK_2(ShopItem::onTouchBegan, this)
listener->onTouchMoved = CC_CALLBACK_2(ShopItem::onTouchMoved, this)
listener->onTouchEnded = CC_CALLBACK_2(ShopItem::onTouchEnded, this)
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, sprite)
在onToucheBegan里面设置精灵的position就好了auto sp = this->getChildByTag(123)//假设精灵tag为123
CCPoint touchLocation = touch->getLocation()// Get the touch position
touchLocation = this->convertToNodeSpace(touchLocation)//转换坐标系
sp->setPosition(touchLocation)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)