Hello Guest

Author Topic: Question: Programmatically change tile map sprites and colors?  (Read 4595 times)

firmie

  • Newbie
  • *
  • Posts: 15
    • View Profile
Question: Programmatically change tile map sprites and colors?
« on: January 05, 2014, 11:03:36 pm »
I feel a bit dense here, but I can’t figure out how to get at specific sprites from a tile map and change their colors.

After loading in my tile map, I want to set a sprite randomly to one of multiple in a set. Then I wan’t to go through and tint the colors slightly just like you can do when editing in the tile map. Is this possible in code?

Thanks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Question: Programmatically change tile map sprites and colors?
« Reply #1 on: January 06, 2014, 11:18:36 am »
Hi,

You can do whatever you like with the tile map at runtime (within reason of course, and realise that updating the collision mesh is really expensive and slow).
http://www.2dtoolkit.com/docs/latest/html/classtk2d_tile_map.html

The functions of interest are:
tileMap.GetTile / SetTile (id = sprite id in the sprite collection).
and tileMap.ColorChannel.GetColor and SetColor.
Once you've changed what you want, be sure to call
tileMap.Build().

firmie

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Question: Programmatically change tile map sprites and colors?
« Reply #2 on: January 08, 2014, 07:39:15 am »
Thank you, that clears some things up. Getting/setting the sprite ID and changing the color channel makes sense.

I’m still a little fuzzy on how to get a specific tk2dSprite reference for a tile, as the ID is not unique per in game sprite object. Can I get a reference to a sprite object at a specific tile?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Question: Programmatically change tile map sprites and colors?
« Reply #3 on: January 08, 2014, 11:47:05 am »
tk2dSprite has no connection to the tiles. The sprite collection (tk2dSpriteCollectionData) contains a list of sprite definitions. the tk2dSprite displays a sprite definition, and the tile map displays a sprite definition as a tile. If you need to get a spriteId by name, etc, you can simply do so from the sprite collection data object (GetSpriteIdByName is the function you want).