Jump to content

Create Objects From Tilemap


Recommended Posts

Hello, I have a tiled map with an object layer but am unsure how to add them...


I have this JavaScript:


var createItems = function(type) {
    //create items
    items = game.add.group();
    items.enableBody = true;
    var item;    
    result = findObjectsByType(type, map, 'Object Layer 1');
      createFromTiledObject(element, items);
  //find objects in a Tiled layer that containt a property called "type" equal to a certain value
  var findObjectsByType = function(type, map, layer) {
    var result = new Array();
      if(element.properties.type === type) {
      console.log("Found a " + type);
        //Phaser uses top left, Tiled bottom left so we have to adjust
        //also keep in mind that the cup images are a bit smaller than the tile which is 16x16
        //so they might not be placed in the exact position as in Tiled
        element.y -= map.tileHeight;
    return result;
  //create a sprite from an object
  var createFromTiledObject = function(element, group) {
    var sprite = group.create(element.x, element.y, element.properties.sprite);
      //copy all properties to the sprite
        sprite[key] = element.properties[key];



But it doesn't fire on account of getting neither no console log nor coins being rendered. Please help!

Link to comment
Share on other sites

    map = game.add.tilemap('map');


    map.setCollisionBetween(1, 12);

    layer = map.createLayer('Tile Layer 1');



    //  Here we create our coins group
    coins = game.add.group();
    coins.enableBody = true;

    //  And now we convert all of the Tiled objects with an ID of 34 into sprites within the coins group
    map.createFromObjects('Object Layer 1', 34, 'coin', 0, true, false, coins);

    //  Add animations to all of the coin sprites
    coins.callAll('animations.add', 'animations', 'spin', [0, 1, 2, 3, 4, 5], 10, true);
    coins.callAll('animations.play', 'animations', 'spin');

You don't need to write custom code to create the objects from within an object layer, phaser takes care of this.

Please see the example here: http://phaser.io/examples/v2/tilemaps/create-from-objects

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...