开发者

Tiled Map Question in Cocos2d for a Vertical Scrolling Game

I created a tile map using Tiled application. The map is 40X40 with the tileset size of 32X32. In the game the map is scrolling downwards giving the illusion that the car is moving. I am having trouble getting the Y coordinates when I click on the map during the game. I need to convert the Cocos2d coordinate system into a tileset system. When the tile map has reached the end I place the car again at the start of the map. This way the map continues infinitely. Inside the Tiled application I can see the coordinate of the block I need to get which is 3,19 but I am having a hard time figuring out how to convert the Cocos2d coordinate to reflect that tile. Here is my code:

- (CGPoint)tileCoordForPosition:(CGPoint)position {

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

      int y1 = ((self.tiledMap.mapSize.height * self.tiledM开发者_Python百科ap.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];
}


Check follow link: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:tiled_maps?s=tmx

Try to get CCTMXLayer object with CCTMXTiledMap's method:

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

Then use one of follow CCTMXLayer's methods:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜