2D Toolkit Forum

2D Toolkit => Support => Topic started by: firmie on January 05, 2014, 11:03:36 pm

Title: Question: Programmatically change tile map sprites and colors?
Post by: firmie 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.
Title: Re: Question: Programmatically change tile map sprites and colors?
Post by: unikronsoftware 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().
Title: Re: Question: Programmatically change tile map sprites and colors?
Post by: firmie 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?
Title: Re: Question: Programmatically change tile map sprites and colors?
Post by: unikronsoftware 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).