最近刚开始学习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);
}
这种姿势是不是有问题?