我正在尝试获取我单击位置的 xy 坐标,或者更好的是鼠标指针所在的位置,以便我可以将它们转换为 char 并将它们显示在屏幕上,我记得 hellolua 中的一些东西抛出坐标clicks and moves while clicked as touch movements into the console,所以我想也许我可以在 cpp 中使用 cctouch 命令 getLocationInView(); 做类似的事情,这就是我所做的。 在初始化中,我声明
setTouchEnabled(true);
以及我试图获取我使用的触摸坐标的位置
CCTouch pTouch;
CCPoint p = pTouch.getLocationInView();
p = CCDirector::sharedDirector()->convertToGL(p);
float x = p.x;
float y = p.y;
但是它并没有像我期望的那样工作。有谁知道在cocos2d-x(cpp)中如何让鼠标/触摸坐标准确返回?
编辑:对不起,我应该澄清一下,我正在 VisualStudio 快速环境中的 Windows 7 桌面上工作,我希望这个项目是跨平台的。样本中的 hellolua 应用程序让鼠标点击将以坐标开始和结束的触摸事件抛出到控制台,即使我使用的是鼠标。这就是为什么我希望我能在 CPP 中做类似的事情,除了在屏幕上打印坐标。但是使用这段代码,我会得到 x 和 y 的静态值,无论我点击哪里,这些值都不会改变。
最佳答案
试试这段代码:
bool PickCard::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
CCPoint touchLocation = touch->getLocationInView();
touchLocation = CCDirector::sharedDirector()->convertToGL( touchLocation );
touchLocation=convertToNodeSpace(touchLocation);
CCLOG(" TouchLocation X=%f TouchLocation Y=%f",touchLocation.x,touchLocation.y);
}
关于c++ - cocos2d-x 中的鼠标/触摸坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16081171/