using a tile from a internal tileset to change an object's image

Hi,

I’ve been trying for some hours now do the following:

change an object’s sprite using a tileset.

specifically, I’m trying to do the following:

  • in Tiled, I’ve created an Object “food”, which has no image whatsoever attached to it

  • in Lua, I’ve used a property listener to get the value of the sprite “food”, which has in the tileset viewer a property called “IsFood”

[lua]local foodObjSpr = function(property, type, object)
foodSprite = object.sprite
end
self.map:addPropertyListener(“IsFood”, foodObjSpr) [/lua]

  • so the global foodSprite obj would be available to change the objects image representation as

[lua]local onFood = function(object)
print(foodSprite.value)
food = display.newImage(foodSprite)
end[/lua]
self.map:addObjectListener(“foodSpawn”, onFood)

what am I missing???

[import]uid: 31814 topic_id: 6472 reply_id: 306472[/import]

Hi,

An Object in Tiled doesn’t have a sprite image from a tileset by default, it is simply a position and size. What you could do is create a Tile the normal way and then you can change the tileset image in code via the setTileAt function - http://justaddli.me/api.php?c=layer&m=setTileAt

Graham [import]uid: 5833 topic_id: 6472 reply_id: 22399[/import]

I’ve just added in a function on the tile itself to change the image now. So if you got access through your tile in any normal way such as a property listener you can then change the time’s image via:

tile:setImage(4)

4 being the GID of a tile in a tileset. [import]uid: 5833 topic_id: 6472 reply_id: 22401[/import]

hi!
this new function will be o use indeed, but what I need is a way to get the sprite from the tileset.

check what I did (it worked, but the image was, off course, removed from the map after colision)

[lua]food.sprite = self.map:getTileAt({row=1, column=1}).sprite[/lua]

if I could get it directly from the tileset, it would be great! [import]uid: 31814 topic_id: 6472 reply_id: 22425[/import]

Would you like a sort of tileSet:createSprite(tileID) type function that would return a new sprite object? [import]uid: 5833 topic_id: 6472 reply_id: 22430[/import]

EXACTLY!

being able to create a sprite from a tileset(tileID) would solve my problem nicely and, I think, is a great addition to lime, since it would allow designers to focus only on creating tilesets.

besides that, an easier way to create objects programmaticly using different sprites is always nice! [import]uid: 31814 topic_id: 6472 reply_id: 22433[/import]

Ask and ye shall receive :slight_smile:

  
--- Creates a new sprite instance from a specific tile in the tileset.  
-- @param gid The gid of the tile.  
-- @usage Originally created by draconar - http://developer.anscamobile.com/forum/2011/02/12/using-tile-internal-tileset-change-objects-image  
function TileSet:createSprite(gid)  
  
 -- Create the sprite instance  
 local sprite = newSprite(self.spriteSet)  
   
 -- Set the sprites frame to the current tile in the tileset  
 sprite.currentFrame = gid - (self.firstgid) + 1  
  
 return sprite  
end  
  

You can then use it like so:

[code]
local tileSet = map:getTileSet(“TileSetName”)

if tileSet then
local newSprite = tileSet:createSprite(5)
newSprite.x = 100
mewSprite.y = 50
end

[/code] [import]uid: 5833 topic_id: 6472 reply_id: 22440[/import]

holy cow!
it worked like a charm!

just a little correction: the variable

[lua]local newSprite = sprite.newSprite[/lua]

should be added to the top of file lime-tileSet.lua.

it wasn’t available in my file.

cheers! [import]uid: 31814 topic_id: 6472 reply_id: 22457[/import]

Awesome, glad it worked and thanks for the suggestion! I can see it coming in use quite a bit so keep 'em coming.

Oh yea, whoops. I forgot about the localised functions. Sorry :slight_smile: [import]uid: 5833 topic_id: 6472 reply_id: 22460[/import]