Object sprites from tileset

First off, I love Tiled!

I’ve trying to figure out how to pull the sprite image data/filename from an object once I’ve got it in code. I’ve got code that technically works, but it requires every single object has a name property with the filename of the sprite. If I forget to add the property or misspell it I get a crash.

Can I pull the tileset filename from an object?

Thank you very much!

Can you be more specific? Since Tiled is cross-platform and not engine-specific, it really helps to know what kind of engine / TMX loader you are using.

As for “objects” do you mean tiles on a tile layer or tile objects on an object layer? Either way, both reference an index in the tileset via GID (global identifier). That means if you have the tileset loaded as texture, the GID references a portion of the texture given the tile (grid) size, the tileset image dimensions, and tileset padding/spacing.

If you refer to “regular” objects (ie polygon, polyline, ellipse, rectangle) then those have no image attribute. If you want to assign images to them, you do have to specify an image name as a property. If that crashes, you should change that code (if possible) to just print a warning to the console and/or load a “missingtexture.png” in place of the actual texture.

Ok sure, and thanks for the reply!

I’m using cocos2dx 2.2 to build a win32 game. I’ve posted the code that grabs my objects from the object layer. I’m not refering to polys since they would have no sprite data associated with them. Instead I’m trying to get the many sprites I have carefully laid out in the objects layer.

Here’s the code

CCTMXObjectGroup * objectsGroup = mWorldTiledMap->objectGroupNamed("Objects");
CCAssert(objectsGroup != NULL, "'Objects' object group not found");

CCArray * objects;
CCObject * it;
objects = objectsGroup->getObjects();

// iterate through the tiled objects and give them ccsprite based on their names
for (int i = 0; i < objects->count(); i++) {
	CCDictionary * objectProperties = (CCDictionary *)objects->objectAtIndex(i);
	const CCString * x = (CCString *)objectProperties->valueForKey("x");
	const CCString * y = (CCString *)objectProperties->valueForKey("y");
	const CCString * gid = (CCString *)objectProperties->valueForKey("gid");
	const CCString * fileName = (CCString *)objectProperties->valueForKey("name");
	CCAssert(fileName != NULL, "You have to include the sprite filename in the Tiled object name field!");

	CCTexture2D * texture = CCTextureCache::sharedTextureCache()->addImage(fileName->getCString());
	CCSprite * sprite = CCSprite::create();


	sprite->setPosition(ccp(x->intValue() + sprite->getContentSize().width / 2, y->intValue() + sprite->getContentSize().height / 2));
	//CC_SAFE_RETAIN(sprite); --------- too many retains? ---------

In that case the gid of the object is what you need. You’ll also need access to the map’s tileset(s) so that you can find the tile (texture and tile rectangle) in the tileset.

Here’s what you should look after:

  • get ahold of the tileset instance or look in the map itself, somewhere must be a reference to either the tileset or its texture
  • check the code that gets a texture or sprite frame for a given gid. Copy and adapt that code, or just use it if possible to get a texture+rect or spriteframe (which is basically a texture+rect).
  • assign the texture or spriteframe to the sprite

I’m afraid I can’t give more help because I’m not very familiar with cocos2d-x.

Well thanks for the effort. That’s what I’ve been looking after. I’m asking where to find that stuff since I can’t find it.