Cocos2dx 3.17 TileMap Basic Sample - wrong tilecord position, object layer itmes position returned also wrong

394 Views Asked by At

I tried cocos2d TileMap sample from here, In this tilecord returned for given player position is wrong.

Here is code

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

Here is Map Screenshot enter image description here

Also tileCord returned is wrong. So collision is not working. I used it from sample in Ray Wenderlich site

Here is full sample code https://app.box.com/s/whunv70tstwxbgzxdvxfeu080y6gwucb

If anyone has time, then check it and please help me to find bug.

1

There are 1 best solutions below

0
Guru On

In cocos2dx 3.17, spawn points returned is exactly half, same tileMap returns proper value in Cocos2d-ObjC project. So temporary fix is to multiply input value inside tileCoordForPosition by 2. This is temporary solution...still waiting for proper fix.

Point HelloWorld::tileCoordForPosition(Point position)
{
    Point newPos = Vec2(position.x*2, position.y*2);

    int x = newPos.x / mTileMap->getTileSize().width;
    int y = ((mTileMap->getMapSize().height * mTileMap->getTileSize().height) - newPos.y) / mTileMap->getTileSize().height;
    return Point(x, y);
}

Here is full working TileMap sample : https://app.box.com/s/r3kglzbx6naig896bq4my7opfeg6ftwz