Tiled Loader - ssk.tiled.*

This module allows yout to create game scenes using the Tiled editor and then load that scene into Corona using default builder factories and/or custom factories of your own design.

This feature should be considered (somewhat) a Work In Progress at this time.

While I have tested it and it is very functional, I am still improving it as I use the module in my own games. I believe there will be additional refinements and feature additions later.

~ The Roaming Gamer

Ponywolf Loader versus SSK 2 Tiled Loader

This tiled loader/tool is different from the one made by Michael Wilson (AKA Ponywolf) in the recent blog series: "Using Tiled for layout and level design." However, you can still load those same levels and operate on them with the SSK 2 Tiled Loader.

Learning To Use Tiled

If you want to learn about editing levels with Tiled first, go take a look at these YouTube videos:

  1. Using Tiled – Part 1: Installation and Tiled Basics
  2. Using Tiled – Part 2: Object Layers versus Tile Layers
  3. Using Tiled – Part 3: Composing Scenes
  4. Using Tiled – Part 4: Custom Object Properties

Loading Level Data

To load a tiled 'level', you need to follow these steps:

  1. Create a level in the tiled editor. If you don't know how to do this see Learning To Use Tiled above.
  2. Export that level as a LUA file.
  3. Create a new 'Tiled Loader' helper object.
  4. Set the data path for the level data.
  5. Load the level data.

-- Create Tiled Loader helper object
local level = ssk.tiled.new()

-- Data file is in folder 'tests/tiled/'
level.setLevelsPath( "tests/tiled" )

-- Load the level file 'dummy.lua' and pass no specific extra settings.
level.load( "dummy" )

Drawing Level Using Default Factories

If you simply want to draw the level on your screen, you can do so quickly with the default factories like this:

  1. Load Level Data as discussed above
  2. Call draw()

level.draw( scengGroup )

This single line of code will draw all objects in the scene with a few exceptions:

Drawing Level Using Custom Factories

While it is cool to draw the scene very fast using the built in draw() function, it isn't useful except for prototyping and verifying layout.

The better way is to use Custom Factories.

This part of the documentation needs improvement. For now, please see the full example at the bottom of the page and also see it running in the validation test that comes with SSK2:

'Tiled Loader Test #1'

~ The Roaming Gamer

Tiled Loader Helper Quick Reference

Syntax Summary
loader.setLevelsPath(levelpath) Set the path to all level files.
loader.load(path) Load a named level file from the levelpath/path. Be sure to exclude the .lua extension.
loader.drawObj(group,rec) Draw the object represented by rec using the default factory object builder and place it in group.
loader.draw(group) Draw all objects in the level using the default factory object builder and place them in group.
loader.forEach(func) Call func( rec, index ) for each object in the level. rec is the record for the current object and index is the numeric index (1, 2, 3, ...) of that object in the level table.
loader.getWidth() Get the calculated width of the tiled level.
loader.getHeight() Get the calculated height of the tiled level.
loader.getImages() Get a table of records containing information about each textured used in the level, its path, width, and height.
loader.getImage(gid) Using the GID value from a object record, request the texture record used by that object which includes the path to the image, and the width and height of that texture.
loader.getImagePath(gid) Using the GID value from a object record, request the path for the textured used by that object.
loader.getRecords() Returns a list of all object records in the tiled level.

Custom Factory Example


local RGTiled = ssk.tiled

-- =============================================================
local test = {}

function test.run( group, params )
   group = group or display.currentStage
   params = params or {}

   -- Create some basic layers.
   --
   local layers = quickLayers( group, 
         "underlay",
         "world",
            { "background", "content", "foreground" },
         "overlay" )   

   --table.dump(RGTiled, nil, "RGTiled")   
   local level = RGTiled.new()

   --table.dump(level, nil, "level")   
   level.setLevelsPath( "tests/tiled" )

   level.load( "dummy", {} )

   --table.dump(level, nil, "level loaded")   

   local objects = level.getRecords()
   --table.dump(objects,nil,"objects")

   local images = level.getImages()
   --table.dump(images,nil,"images")

   local function simpleSpinner( obj )
      obj.rotation = 0
      obj.onComplete = simpleSpinner
      local rec = obj.rec
      local properties = obj.rec.properties
      transition.to( obj, { rotation = 360, time = properties.rotTime or 1000, onComplete = obj } )
   end

   local function func( rec, num )
      local img = level.getImage( rec.gid )
      local obj = newImageRect( layers[rec.layer], rec.x, rec.y, img.image, 
         { w = rec.width, h = rec.height, rec = rec }  )
      if( rec.flip.x ) then obj.xScale = -obj.xScale end
      if( rec.flip.y ) then obj.yScale = -obj.yScale end
      --table.dump(rec)

      if(rec.name == "spinner") then
         simpleSpinner( obj )
      end

   end

   level.forEach( func )
end

return test



RoamingGamer Copyright © Roaming Gamer, LLC. 2008-2016; All Rights Reserved