如何获得触摸和图层对象的绝对位置? [英] How to get absolute positions of touches and layer objects?

查看:157
本文介绍了如何获得触摸和图层对象的绝对位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图检测哪个字符(目前是CCLayer扩展对象)我碰到cocos2d-x有一个奇怪的问题。问题是,我点击的sprite的位置从来不匹配注册的触摸位置。

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.

关于如何在ccTouchesBegan中检测到地图(CCLayer)在同一个scale比字符(也是CCLayer)触摸的任何想法?在我收到触摸图像的地图中,如何获得绝对位置(我将字符移动到点击位置)?

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)?

我知道他们可能是非常基本的问题,但我一直在寻找答案一些小时,我找不到解决方案。任何对cocos2d-x或cocos2d的建议都是受欢迎的。

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.

提前感谢!

推荐答案

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 - 指向世界坐标, CCTouch :: getLocation()方法在您的触摸侦听器。

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

这篇关于如何获得触摸和图层对象的绝对位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆