刚学习cocos,按照mercuryrozen瓦片地图教程自己跟到做了一个,但是碰撞判断有问题啊。
1、地图下方,感觉坐标都有问题了,下方的碰撞判断有问题;
2、左边的障碍物这一列居然在地图中不显示了;
3、上边和右边的地图碰撞是对的。
附上主要代码:
bool HelloWorld::init()
{
std::string file = “first.tmx”;
auto str = String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename(file.c_str()).c_str());
_tileMap = TMXTiledMap::createWithXML(str->getCString(), “”);
_background = _tileMap->getLayer(“background”);
_blockage = _tileMap->getLayer(“blockage01”);
//_blockage->setVisible(false);
addChild(_tileMap, -1);
TMXObjectGroup* objects = _tileMap->getObjectGroup("Object-Player");
CCASSERT(NULL != objects, "'Object-Player' object group not found");
auto playerShowUpPoint = objects->getObject("PlayerShowUpPoint");
CCASSERT(!playerShowUpPoint.empty(), "'PlayerShowUpPoint not found");
int x = playerShowUpPoint"x"].asInt();
int y = playerShowUpPoint"y"].asInt();
_player = Sprite::create("029.png");
_player->setPosition(x + _tileMap->getTileSize().width / 2 / Director::getInstance()->getContentScaleFactor(),
y + _tileMap->getTileSize().height / 2 / Director::getInstance()->getContentScaleFactor());//将逻辑坐标转换为GLVIEW坐标
_player->setScale(1.0 / Director::getInstance()->getContentScaleFactor());
//_player->setPosition(x + _tileMap->getTileSize().width / 2, y + _tileMap->getTileSize().height / 2);
//_player->setScale(0.5);
addChild(_player);
setViewPointCenter(_player->getPosition());
auto listener = EventListenerMouse::create();
listener->onMouseDown = CC_CALLBACK_1(HelloWorld::OnMouseClickDown, this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
void HelloWorld::OnMouseClickDown(cocos2d::Event* event){
EventMouse* e = (EventMouse*)event;
auto actionTo1 = RotateTo::create(0, 0, 180);
auto actionTo2 = RotateTo::create(0, 0, 0);
auto ploc = _player->getPosition();
auto mloc = this->convertToNodeSpace(e->getLocation());
auto dif = mloc - ploc;
ploc = ploc * Director::getInstance()->getContentScaleFactor();//将GLVIEW坐标转换成地图逻辑坐标
if (abs(dif.x) > abs(dif.y)){
if (dif.x > 0){
ploc.x += _tileMap->getTileSize().width;
_player->runAction(actionTo2);
}
else{
ploc.x -= _tileMap->getTileSize().width;
_player->runAction(actionTo1);
}
}
else{
if (dif.y > 0){
ploc.y -= _tileMap->getTileSize().height;
}
else{
ploc.y += _tileMap->getTileSize().height;
}
}
if (ploc.x <= (_tileMap->getMapSize().width * _tileMap->getMapSize().width) &&
ploc.y <= (_tileMap->getMapSize().height * _tileMap->getMapSize().height) &&
ploc.y >= 0 &&
ploc.x >= 0){
this->setPlayerPosition(ploc);
}
else{
ploc.x = (_tileMap->getMapSize().width * _tileMap->getMapSize().width) / 2;
ploc.y = (_tileMap->getMapSize().height * _tileMap->getMapSize().height) / 2;
this->setPlayerPosition(ploc);
}
this->setViewPointCenter(_player->getPosition());
}
void HelloWorld::setPlayerPosition(Point position)
{
Point tileCoord = this->tileCoordForPosition(position);
int tileGid = _blockage->getTileGIDAt(tileCoord);
if (tileGid) {
auto properties = _tileMap->getPropertiesForGID(tileGid).asValueMap();
if (!properties.empty()) {
auto collision = properties"Blockage"].asString();
::MessageBoxA(NULL, collision.c_str(), “”, MB_OK);
if (“true” == collision) {
return;
}
}
}
_player->setPosition(position/Director::getInstance()->getContentScaleFactor());
//_player->setPosition(position);
}
Point HelloWorld::tileCoordForPosition(Point position)
{
//int x = position.x / _tileMap->getTileSize().width;
int x = ((_tileMap->getMapSize().width * _tileMap->getTileSize().width - 1) - position.x) / _tileMap->getTileSize().width;
int y = ((_tileMap->getMapSize().height * _tileMap->getTileSize().height-1) - position.y) / _tileMap->getTileSize().height;
return Point(x, y);
}
void HelloWorld::setViewPointCenter(Point position){
auto winSize = Director::getInstance()->getWinSize();
int x = MAX(position.x, winSize.width / 2);
int y = MAX(position.y, winSize.height / 2);
x = MIN(x, (_tileMap->getMapSize().width * this->_tileMap->getTileSize().width) - winSize.width / 2);
y = MIN(y, (_tileMap->getMapSize().height * _tileMap->getTileSize().height) - winSize.height / 2);
auto actualPosition = Point(x, y);
auto centerOfView = Point(winSize.width / 2, winSize.height / 2);
auto viewPoint = centerOfView - actualPosition;
this->setPosition(viewPoint);
}
具体请查看附件。谢谢各位!
