cocos3.16下的内容缩放因子处理问题

最近刚开始学习cocos,版本也比较新,网上找不到比较新的教程,学习https://blog.csdn.net/llkk890320/article/details/25970617的例子的时候发现3.16自带的缩放因子需要在自己加载TiledMap地图之后每次都手动处理缩放因子的计算,比如下面的代码,根据触摸点的位置计算忍者应该移动的位置

void HelloWorld::touchEnded(cocos2d::Touch *touch, cocos2d::Event *event) {
    Point touchLocation = touch->getLocationInView();
    touchLocation = Director::getInstance()->convertToGL(touchLocation);
    touchLocation = this->convertToNodeSpace(touchLocation);

    Point playerPos = _player->getPosition();
    Point diff = touchLocation - playerPos;

    if (abs(diff.x) > abs(diff.y)) {
        if (diff.x > 0) {
            playerPos.x += _tileMap->getTileSize().width / Director::getInstance()->getContentScaleFactor();
        } else {
            playerPos.x -= _tileMap->getTileSize().width / Director::getInstance()->getContentScaleFactor();
        }
    } else {
        if (diff.y > 0) {
            playerPos.y += _tileMap->getTileSize().height / Director::getInstance()->getContentScaleFactor();
        } else {
            playerPos.y -= _tileMap->getTileSize().height / Director::getInstance()->getContentScaleFactor();
        }
    }

    if (playerPos.x >= 0 && playerPos.y >= 0
        && playerPos.x <= _tileMap->getTileSize().width * _tileMap->getMapSize().width / Director::getInstance()->getContentScaleFactor()
        && playerPos.y <= _tileMap->getMapSize().height * _tileMap->getMapSize().height / Director::getInstance()->getContentScaleFactor()) {
        this->setPlayerPosition(playerPos);
    }

    this->setViewPointCenter(playerPos);
}

这种姿势是不是有问题?

cocos2d::Point HelloWorld::tileCoordForPosition(cocos2d::Point position) {
    
    int x = (int) (position.x * Director::getInstance()->getContentScaleFactor() / _tileMap->getTileSize().width);
    int y = (int)((_tileMap->getTileSize().height * _tileMap->getMapSize().height - position.y * Director::getInstance()->getContentScaleFactor()) / _tileMap->getTileSize().height);
    
    return Point(x, y);
}

这是根据屏幕位置计算TiledMap位置的函数,也需要这样手动计算内容缩放因子

是的,tilemap的解析这里是有问题,只不过没什么人用就没人提issue了

那我应该换个cocos的版本吗

自己简单改下源码吧,印象中应该不难改的

能给下改的思路吗?是哪一块出问题了

具体记不清了,我遇到类似问题还是两年前,你去源码tmx相关的文件里面查查类似CC_SIZE_PIXELS_TO_POINTS之类的宏,大概是这样的转换导致的。