Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I center a small CCTMXTileMap in cocos2d?
    text
    copied!<p>I'm making a tile-based 2d RPG with cocos2d. I am using CCTMXTileMaps for my maps. My player is centered on the the screen and the map moves around it (except when the player walks towards the edge of the map, where they walk away from center and actually move). This system works well in large maps. In small maps however, the map is anchored to the top right of the screen. While the mechanics still work, it would be nice to have those small maps automatically centered.</p> <p><strong>How can I make maps that are smaller than the screen's dimensions be centered?</strong> I want the map to be centered in the "red box". (The red box is just here for illustrative purposes, it's not actually there in code.)</p> <p><img src="https://i.stack.imgur.com/aRMsP.png" alt="enter image description here"></p> <p><strong>EDIT:</strong></p> <p>So I figured out how to do it in theory, but I'm having trouble understanding the coordinate system. I'm using the following code to center the map but it's not behaving as expected. The map loads off of the screen.</p> <pre><code> if ((self.tileMap.contentSize.height &lt; screenSize.height) &amp;&amp; (self.tileMap.contentSize.width &lt; screenSize.width)) { CGSize winSize = [[CCDirector sharedDirector] winSize]; CGPoint centerOfView = ccp(winSize.width/2, winSize.height/2); CGPoint centerOfMap = CGPointMake((self.tileMap.mapSize.width*self.tileMap.tileSize.width)/2, (self.tileMap.mapSize.height*self.tileMap.tileSize.height)/2); self.tileMap.anchorPoint = centerOfMap; self.tileMap.position = centerOfView; } </code></pre> <p>What am I doing wrong here?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload