I'm creating a simple game for the iPhone using Cocos2d. I'm having a hard time understanding how add real objects to a tiled map.

I know that you can add an object-layer to the tiled map and set the position of it, but I'm not sure how to define WHAT object it should be.

For example, if I'm creating a simple Super Mario Bros. I want to add different blocks to the map, one should hold a star, one should hold a coin, and so on.

How should I define what object it is?

So my question, how do I correctly create class-objects in a tiled map?

  • \$\begingroup\$ Hey, NSAddict. Do you want to know how to create the object layer, how to create the game objects or how to place them in this layer? I don't think I fully understand your question so if you could clarify it, I can edit my answer to help you better. Thanks. \$\endgroup\$ Feb 3, 2013 at 12:33
  • \$\begingroup\$ @LucasTulio Sorry if I was unclear. I understand how to create a new layer, but I'm not sure how to create and loop though the objects. \$\endgroup\$
    – IluTov
    Feb 3, 2013 at 12:41
  • \$\begingroup\$ You want to loop through which objects? The Tiles in the map or the gameplay objects (star, coin)? The Tiles should have a simple bidimensional int array (x, y) that you can loop through. If you want to loop through a list of your gameplay objects, you should create this array and add all of your coins and stars and mushrooms to it. That way you can always loop through them. \$\endgroup\$ Feb 3, 2013 at 12:52
  • \$\begingroup\$ I mean gameplay objects like star, coin... So I have to loop though the whole map, even if for example only 1 object is added to the map? \$\endgroup\$
    – IluTov
    Feb 3, 2013 at 12:54
  • \$\begingroup\$ Why would you want to loop through them? \$\endgroup\$ Feb 3, 2013 at 12:59

1 Answer 1


First, you should have a top layer in your tiled map. This layer will have the same amount of tiles (x, y) as the rest of your map and should be rendered last. In my game, I created a placeEntity(Entity e, int xTile, int yTile) method so you can add the gameplay objects anywhere you want in the map.

About the Stars and Coins, they're just the Star and Coin objects placed at an x and y position using the above method.

  • \$\begingroup\$ Yes I understand, but how do I loop though the objects? I also saw that you can give them properties, there I could set the entity. \$\endgroup\$
    – IluTov
    Feb 3, 2013 at 12:33
  • \$\begingroup\$ Have you seen this tutorial here raywenderlich.com/1163/… it goes through finding the objects in the object layer and doing something with it. I.e spawn point etc... \$\endgroup\$
    – Savlon
    Feb 3, 2013 at 23:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .