Ponytiled / Sticker Knight: How to add new objects to the map while playing?

Dear Corona community,

I have been playing around with Corona for some time now, trying to build a jump 'n run game based on the Sticker Knight template.

Sticker Knight as you might know uses the Ponytiled library to read Tiled maps. I am looking for a way to add new objects to the map while playing, like for enemies dropping items and such. I can’t seem to find out how to do that as I can’t figure out how to access the display groups of the map to insert new objects.

The map is stored in a variable called “map” like his:
map = tiled.new( mapData, “scene/game/levels” )

Is it possible to access the individual display groups (aka map layers) “inside” the map data during the game and add new objects?

Any explanation or tips on how to do this would be greatly appreciated.

Thanks
ktgt

Hey, sorry for the delay. I’ve been on the road for a while.

You can get to any layer via map:findLayer(“name”) and put new display objects in that layer, that’s also in the map. The x/y will be local to the map, so if you add an image and insert it into the layer you can set its x/y in map coordinates.

I believe the GitHub has an example.

Thanks a lot, Michael, now that’s some first hand Corona advice. :slight_smile: Using your suggestion, I managed to insert an image into the correct map layer. I made a new function inside ponytiled.lua like below and called that function from game.lua (in my test scenario I simply called it via a button), which works well.

function map:insertObject(x,y,layername)
  layer = map:findLayer(layername)
  newobject = display.newImageRect( “scene/game/img/enemy.png”, 69, 56 )
  newobject.x = x
  newobject.y = y
  layer:insert(newobject)  
end

But now I am stuck figuring out how to insert not just an image, but an actual game object. Let’s say I have an enemy.lua like in Sticker Knight. I want to add that to the map in the same way as described above. How would I do that? Couldn’t find a way to do it in the Sticker Knight template.

Hi @ktqt,

Simple way to add new character to map in run time (tested with SKP template, game.lua)

-- Put this code after map:extend function map:insertObject( x, y, layerName )     layer = self:findLayer( layerName )     return display.newRect( layer, x, y, 200, 100 ) end function map:extendObject( object, extension )     -- Load module     local plugin = require ( self.extensions .. extension )     if object then          -- Extend the display object with its own custom code        object = plugin.new( object )     end   end local object = map:insertObject( hero.x + 300, hero.y, 'game' ) map:extendObject( object, 'enemy' )

Have a nice day:)

Good luck.

ldurniat

Thanks a lot, that does help a lot! I inserted the two functions into ponytiled.lua and the function calls in game.lua. However, the functions only seem to work when called from game.lua. If I try to call them from another module (like from hero.lua or enemy.lua), it doesn’t work. I am generally having lots of trouble with modules and how variables and functions are “visible” across modules. <_< Can you help me out?

The map object is not defined in hero.lua (and enemy.lua) That is why it don’t work.

I recommended you these tutorials  :

Good luck @ktgt

ldurniat

Hey, sorry for the delay. I’ve been on the road for a while.

You can get to any layer via map:findLayer(“name”) and put new display objects in that layer, that’s also in the map. The x/y will be local to the map, so if you add an image and insert it into the layer you can set its x/y in map coordinates.

I believe the GitHub has an example.

Thanks a lot, Michael, now that’s some first hand Corona advice. :slight_smile: Using your suggestion, I managed to insert an image into the correct map layer. I made a new function inside ponytiled.lua like below and called that function from game.lua (in my test scenario I simply called it via a button), which works well.

function map:insertObject(x,y,layername)
  layer = map:findLayer(layername)
  newobject = display.newImageRect( “scene/game/img/enemy.png”, 69, 56 )
  newobject.x = x
  newobject.y = y
  layer:insert(newobject)  
end

But now I am stuck figuring out how to insert not just an image, but an actual game object. Let’s say I have an enemy.lua like in Sticker Knight. I want to add that to the map in the same way as described above. How would I do that? Couldn’t find a way to do it in the Sticker Knight template.

Hi @ktqt,

Simple way to add new character to map in run time (tested with SKP template, game.lua)

-- Put this code after map:extend function map:insertObject( x, y, layerName ) &nbsp; &nbsp; layer = self:findLayer( layerName ) &nbsp; &nbsp; return display.newRect( layer, x, y, 200, 100 ) end function map:extendObject( object, extension ) &nbsp; &nbsp; -- Load module &nbsp; &nbsp; local plugin = require ( self.extensions .. extension ) &nbsp; &nbsp; if object then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Extend the display object with its own custom code &nbsp; &nbsp; &nbsp; &nbsp;object = plugin.new( object ) &nbsp; &nbsp; end&nbsp;&nbsp; end local object = map:insertObject( hero.x + 300, hero.y, 'game' ) map:extendObject( object, 'enemy' )

Have a nice day:)

Good luck.

ldurniat

Thanks a lot, that does help a lot! I inserted the two functions into ponytiled.lua and the function calls in game.lua. However, the functions only seem to work when called from game.lua. If I try to call them from another module (like from hero.lua or enemy.lua), it doesn’t work. I am generally having lots of trouble with modules and how variables and functions are “visible” across modules. <_< Can you help me out?

The map object is not defined in hero.lua (and enemy.lua) That is why it don’t work.

I recommended you these tutorials  :

Good luck @ktgt

ldurniat