Jump to content

Creating a sprite from a Tilemap's tileset


Recommended Posts

I have a tilemap that's working fine. What I want to do is to create a Sprite that can be based on the same tileset as the tilemap uses so I can create dynamic tiles.


I've extracted some of my code from some objects below to make what I'm effectively doing at the moment a bit clearer.

var map = game.add.tilemap(mapId);map.addTilesetImage(tilesetName, imageId);var carriedTile = new Phaser.Sprite(game, 100, 64, imageId);game.add.existing(carriedTile);

What I get, as expected, is a Sprite that displays the entire tileset image. What I need to do is to get a Sprite that only displays one tile from the Tileset. How do I achieve that? I've tried setting the textureRegion, but that doesn't seem to do anything.



Link to comment
Share on other sites

Thanks Karma Octopus. Unfortunately, not quite. The tileStrip is a sprite using the same tileset image as the map, but the individual tiles on mouse down are created using the index into the tileStrip rather than creating new Sprites; the tileStrip is still the size of the entire tileset image. My problem would be a bit like trying to make the tileStrip the size of one tile and then able to change the tile it shows.


Mind you the example showed me a couple of other things I didn't know.

Link to comment
Share on other sites

  • 5 weeks later...

Hello there. I'm too want to do the same.

There is an option of loading your tilemap's tileset as a spritesheet:


game.load.spritesheet("tilesetname", "tileset_path", tilewidth, tileheight, frameMax)


and then referring to it while creating the sprite like:


game.add.sprite(x, y, "tilesetname", frameIndex)


However this seems to be more of a workaround than a proper solution.

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...