Mos*_*she 5 cocos2d-iphone ios
我正在使用cocos2d制作基于磁贴的2D动画片.我在我的地图上使用CCTMXTileMaps.我的播放器以屏幕为中心,地图围绕它移动(除非玩家走向地图的边缘,他们离开中心并实际移动).该系统适用于大型地图.但是,在小地图中,地图会锚定在屏幕的右上角.虽然机制仍然有效,但让这些小地图自动居中会很不错.
如何制作小于屏幕尺寸的地图?我希望地图在"红框"中居中.(红色框仅用于说明目的,实际上并不存在于代码中.)

编辑:
所以我在理论上想出了如何做到这一点,但我无法理解坐标系.我正在使用以下代码将地图居中,但它没有按预期运行.地图加载到屏幕上.
if ((self.tileMap.contentSize.height < screenSize.height) && (self.tileMap.contentSize.width < 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;
}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
好的,解决方案非常简单,尽管我遗漏了一些东西。将地图在父级中居中的最基本代码是这样的:
//
// Get the center of the
// screen/map layer (which are the same)
//
CGPoint centerOfSelf = CGPointMake(self.contentSizeInPixels.width/2, self.contentSizeInPixels.height/2);
//
// Position the map in the center
//
tileMap.position = CGPointMake(centerOfSelf.x - (tileMap.contentSizeInPixels.width/2), centerOfSelf.y - (tileMap.contentSizeInPixels.height/2));
Run Code Online (Sandbox Code Playgroud)
如果每个场景加载一张地图,上述代码将起作用。但是,如果您(像我一样)有一个加载地图和相关数据的“世界”场景,那么您需要做一些不同的事情。您会看到,加载大于屏幕的地图将导致图层“拉伸”并调整自身大小以适应更大的地图。这样做时,上面的代码将在“拉伸”的地图场景中居中并随后加载地图。因此,为了解决这个问题,我首先必须调整“世界场景”图层的大小以匹配屏幕的大小。为此,我使用以下代码:
//
// Reset the size of the bounding
// area so that we can properly
// center maps that are smaller than
// the bounding area of the screen.
//
CGSize screenSize = [[CCDirector sharedDirector] winSize];
self.contentSizeInPixels = screenSize;
//move camera
self.position = CGPointZero;
Run Code Online (Sandbox Code Playgroud)
请注意,我还将“摄像机”移回 0,0 位置,以防我们在跟随主角时移开。