cocos2dx版本: github最新提交
平台版本:mac
简单的在scene里添加了鼠标事件监听,然后在回调函数中获取鼠标点击坐标,使用的是getLocation,期望获得的是opengl坐标系的坐标,可为什么实际返回的是屏幕坐标系的坐标?
代码如下
bool GameScene::init() {
if ( !Scene::init() )
{
return false;
}
// LayerColor *_bgColor = LayerColor::create(Color4B::RED);
// this->addChild(_bgColor , -10);
// map_ = TMXTiledMap::create(“res/game.tmx”);
// addChild(map_);
player_ = Sprite::create("res/game.png", Rect(0, 0, 16, 16));
addChild(player_);
auto listener = EventListenerMouse::create();
listener->onMouseDown = CC_CALLBACK_1(GameScene::onMouseDown, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
void GameScene::onMouseDown(Event *event) {
auto *e = dynamic_cast<EventMouse *>(event);
switch (e->getMouseButton()) {
case EventMouse::MouseButton::BUTTON_LEFT: {
auto p = e->getLocation();
printf("%f %f\n", p.x, p.y);
break;
}
default:
break;
}
}
点击游戏左下角,期望获得opengl坐标系坐标,在(0, 0)附近。可实际getLocation返回的是屏幕坐标系,坐标在(0, 320)附近,320刚好是窗口高度。