How to get absolute positions of touches and layer objects?

假如想象 提交于 2020-01-16 12:18:06

问题


I'm having a weird issue with cocos2d-x when trying to detect which character (currently a CCLayer-extending object) I'm touching. The problem is that the location of the sprite I'm clicking on never matches the touch location that is registered.

I've tried different conversion functions but neither of them seem to work.

Any idea about how can I detect in ccTouchesBegan where a map (CCLayer) is being touched in the same 'scale' than the characters (also CCLayer's)? How can I get the absolute position in the map of the touch poisition as I receive it (I will move the character to the clicked position)?

I know that they may be very basic questions, but I've been looking for the answer for some hours and I can't find the solution. Any suggestion either for cocos2d-x or cocos2d is really welcome.

Thanks in advance!


回答1:


1) In order to detect whether world point is in the node, I use following function:

    
    bool VUtils::isNodeAtPoint(cocos2d::CCNode* node, cocos2d::CCPoint& touchLocation) {                
        CCPoint nodePoint = node->convertToNodeSpace(touchLocation);
        CCRect rect = CCRectMake(0, 0, node->getContentSize().width, node->getContentSize().height);
        return rect.containsPoint(nodePoint);
    }

2) touchLocation - is point in world coordinates, in order to get it use CCTouch::getLocation() method in your touch listeners.



来源:https://stackoverflow.com/questions/17892778/how-to-get-absolute-positions-of-touches-and-layer-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!