2011-05-12 27 views
2

我使用Tiled應用程序創建了一個地圖地圖。地圖是40X40,地圖大小爲32X32。在遊戲中,地圖向下滾動,給出了汽車正在移動的錯覺。當我在遊戲中點擊地圖時,我無法獲得Y座標。我需要將Cocos2d座標系轉換爲tileset系統。當瓷磚地圖到達結尾時,我再次將汽車放在地圖的開始處。這樣地圖就會無限地延續下去。在Tiled應用程序中,我可以看到我需要獲取的塊的座標,它是3,19,但我很難弄清楚如何轉換Cocos2d座標以反映該塊。這裏是我的代碼:Cocos2d中的垂直滾動遊戲中的平鋪地圖問題

- (CGPoint)tileCoordForPosition:(CGPoint)position { 

    int x = position.x/self.tiledMap.tileSize.width; 

     int y1 = ((self.tiledMap.mapSize.height * self.tiledMap.tileSize.height) - position.y)/self.tiledMap.tileSize.height; 

    return ccp(x, y1); 
} 

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:[touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location];  

    CGPoint tileCoord = [self tileCoordForPosition:location]; 
} 

回答

0

檢查如下鏈接:http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:tiled_maps?s=tmx

試圖讓CCTMXLayer對象與CCTMXTiledMap的方法:

/** return the TMXLayer for the specific layer */ 
-(CCTMXLayer*) layerNamed:(NSString *)layerName; 

然後使用遵循CCTMXLayer的方法之一:

-(CCSprite*) tileAt:(CGPoint)tileCoordinate; 
-(uint32_t) tileGIDAt:(CGPoint)tileCoordinate; 
-(uint32_t) tileGIDAt:(CGPoint)pos withFlags:(BOOL) flags;