Saturn
Saturn

Reputation: 18149

Iterate through all tile properties in layer

I managed to iterate through all CCSprites (tiles) in a Tiled layer. However, what I really need it is to iterate through all the properties (NSDictionaries) of all tiles within the layer. How would I do such? I don't really need to get the CCSprites, just the property list.

Upvotes: 3

Views: 1558

Answers (1)

Lukman
Lukman

Reputation: 19164

You need to use -(NSDictionary*)propertiesForGID:(unsigned int)GID method for CCTMXTiledMap to get the tile properties.

But first you need to know the GID of the tile. Get that from -(uint32_t) tileGIDAt:(CGPoint)pos method for CCTMXLayer:

CGPoint pos = ccp(2,1);
uint gid = [layer tileGIDAt:pos];
if (gid > 0) {
    NSDictionary *tileProperty = [tiledMap propertiesForGID:gid];

    // do stuff here
}

EDIT: here is how to iterate through all tiles on a CCTMXLayer:

for (NSUInteger y = 0; y < tmxLayer.layerSize.height; y++) {
    for (NSUInteger x = 0; x < tmxLayer.layerSize.width; x++) {
        NSUInteger pos = x + tmxLayer.layerSize.width * y;
        uint32_t gid = tmxLayer.tiles[pos];
        if (gid > 0) {
            NSDictionary *tileProperty = [tiledMap propertiesForGID:gid];

            // do stuff here
        }
    }
}

Upvotes: 7

Related Questions