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"

1
2
3
4
local foodObjSpr = function(property, type, object)
                foodSprite = object.sprite              
   end
self.map:addPropertyListener("IsFood", foodObjSpr) 

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

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.

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)

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

Would you like a sort of tileSet:createSprite(tileID) type function that would return a new sprite object?

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!

Ask and ye shall receive :-)

1
2
3
4
5
6
7
8
9
10
11
12
13
--- 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

holy cow!
it worked like a charm!

just a little correction: the variable

1
local newSprite = sprite.newSprite

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 :-)

views:1475 update:2011/10/13 16:39:51
corona forums © 2003-2011