Having issues with tilemaps

Hey guys!

I’m new to these forums and I’m just getting started learning to work with Cocos2d-x. I’ve been following this C++ tutorial on working with TileMaps:

I have a tilemap which is 32px times 50 tiles - 1600x1600 px. When I try to calculate the position of the tilemap layer so that it’s positioned properly relative to the player I get a bunch of weird issues. I believe it has something to do with support for high dpi screens but it’s weird since my laptop’s screen is just 13" and 1600x900, so it shouldn’t be considered “high dpi”. I’ll share some pictures to show my problem (My code is exactly the same as in the tutorial)…

This is my result when I run the code from the tutorial as it is (the layer is positioned way out of view):

If I alter the setPosition call in the positioning code from the tutorial:

void HelloWorld::setViewPointCenter(CCPoint position) {

    CCSize winSize = CCDirector::sharedDirector()->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);
    CCPoint actualPosition = ccp(x, y);

    CCPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
    CCPoint viewPoint = ccpSub(centerOfView, actualPosition);
    this->setPosition(viewPoint);
}

So that the end of it looks like this instead:

    ...
    CCPoint viewPoint = ccpSub(centerOfView, actualPosition);
    this->setPosition(viewPoint.x/2, viewPoint.y/2);

I get a more accurately positioned layer (but not completely and I also have some rendering artifacts + it’s not scaled correctly if you refer to the TileMap editor’s view):

That got me thinking that there must be some issue with the scaling and just as a test I set the scale of the tilemap node to 2.0 and got this result (and also reverted the previous changes to setPosition):

Now the view is position correctly relatively to the player but it looks weird with the scaling. The odd thing is that I haven’t found a reference to someone else experiencing exactly the same issue, similar issues with positioning errors but none I can correctly correlate to this.

My setup:
Ubuntu 13.04 x64
Cocos2d-x v3.0alpha0-pre
Intel HD 4000 Graphics

I’m thankful for all ideas an input. Thank you for helping me out!


pic1.png (195.3 KB)


pic2.png (383.9 KB)


pic3.png (329.5 KB)

I followed that tutorial and my results work exactly as I expected them to with any scaling I choose to set and function very well on all device resolutions.

Your results appear to be related to, I’m guessing, an incorrect anchor or something.

My code was exactly the same as the tutorial as well.

What cocos2d-x version did you use? The 3.0 alpha or 2.x? Also, what OS did you do this on, it might be a platform specific implementation issue in cocos2d-x :confused:

I used 2.1.2 - 2.1.5 and all platforms (Android 2.x-4.x, Win32, Linux, Mac OSX, BB, OUYA, etc)

PS: If you’re having node positioning issues, it’s not a platform bug :wink:

Ok, I guess I’ll try building from source and see if the bug has been fixed…

Best of luck fixing your code — I am sure you’ll get the bug figured out if you work at it.

My suggestion would be start with a simple case and work up from there until you identify the misunderstanding that was causing the issues you posted above.