0

I'm trying to find the position in pixels of a particular tile in my .tmx map. The method positionAt: of the CCTMXLayer class should do the trick, but whenever I try to use it, it returns a CGPoint with coordinates (0,0).. This is part of the code I'm using.

    // CCTMXTiledMap Declaration using my class MapLayer
    //which inherits from CCTMXTiledMap
    MapLayer *tileMap = [MapLayer node];
    CCTMXLayer *myLayer = [tileMap layerNamed: @"Middle"];
    NSLog(@"%i", [myLayer positionAt: CGPointMake(x,y)].x);
    NSLog(@"%i", [myLayer positionAt: CGPointMake(x,y)].y);

No matter what the values for x and y are for the CGPointMake, the values for the CGPoint (0,0) keep getting logged and I don't understand why.

EDIT:

Am I just declaring my layer with the map wrong? This is my code.

    //init method for MapLayer.m class file 
    @implementation MapLayer 
    -(id) init { 
    if( (self=[super initWithTMXFile: @"ZombearTilemap.tmx"])) { 
    } 
    return self; 
    } 

    //in my MainScene.m class init method
    MapLayer *tileMap = [MapLayer node]; 
    [self addChild: tileMap];
    CCTMXLayer *tileMapLayer = [tileMap layerNamed: @"Middle"];

    // (tileMapLayer == nil) returns false

    //tileMap.mapSize.width returns 0
    //[[tileMapLayer tileAt:ccp(x,y)] boundingBox].size.height returns 0

1 Answers1

0

Couple things to check...

  1. Is myLayer valid (not nil)? If it's nil, you don't have a layer named "Middle" in your TMX file or your TMX file isn't being loaded.
  2. Are myLayer.mapTileSize.width and myLayer.mapTileSize.height non-zero? If they're zero then something is very broken with either your TMX file and/or your subclass of CCTMXTiledMap.

Advice in general: Cocos2d is open source! Set a breakpoint, step into the CCTMXLayer's code for -(CGPoint) positionAt:(CGPoint)pos and find out whats going on in there.


EDIT:

If you're using "Tiled" to create your TMX files, go into Preferences and change "Store tile layer data as:" from "Base64 (gzip compressed)" to "XML". Cocos2d cannot read this TMX encoding, but this lets you easily see the data by opening the TMX file in any sort of text editor or web browser.
(If you're not using Tiled, I have no idea how you're creating TMX files, but go get it here: http://www.mapeditor.org/ )

After changing the preferece, open and re-save your TMX file to somewhere else. You should probably rename it to -XML.tmx or something, so you don't accidentally try to feed it to cocos2d later and wonder why its not loading. ;)

Your Layer's get their mapTileSize property from the map's tileSize property. This property gets set from your TMX file's element. Here's an example: <map version="1.0" orientation="orthogonal" width="160" height="96" tilewidth="16" tileheight="16">

If everything looks good there, then you are accidentally resetting this value to 0, somewhere in your code. If its messed up, then you need to either tweak this xml file manually, or recreate your TMX file from scratch with correct tilesize values.

Don't forget to change your preferences in Tiled back to "Base64 (gzip compressed)" to save your work in a format cocos2d can load.

MechEthan
  • 5,703
  • 1
  • 35
  • 30
  • My layer file isn't nil. I checked it with an if statement. You're right about the height and width though. They're both 0 so somethings broken, I'm just not sure what.. Is there something I could be missing in exporting my tmx file that breaks it because the tmx file shows up, I just can't get its properties to work. – user1371455 May 10 '12 at 00:05
  • @user1371455 can you edit your original question-post and add that code? its not making sense to me. – MechEthan May 12 '12 at 18:13
  • I figured out my problem. I was casting the numbers wrong when I would use NSLog. I was using %i and %d and getting strange results. When i used %1.x, the numbers started showing up correct. – user1371455 May 15 '12 at 02:13