Facebook Twitter E-mail RSS
Home Level Director X Library Module – Corona SDK

Library Module – Corona SDK

Level Director X ships with two library modules which act as support files for when using the Corona SDK export template;

LD_LoaderX.lua (Graphics 2.0)

LD_HelperX.lua (Graphics 2.0)

These can be found in the Utils folder in each of the example projects and the new project template.

LD_LoaderX

This helps with the loading of the levels and exposes helper functions.

So the first step is to make sure this file exists in your project folder (or lib folder as per example below)  and then require the file;

require("lib.LD_LoaderX")

Now you can instantiate a new loader class and load your level into a local variable;

local myLevel = {}
myLevel= LD_Loader:new()
myLevel:loadLevel("Level01")

If you intend to use multiple level then use this to remove the level;

myLevel:removeLevel()

Once you have finished with your all your levels (or have 1) you can use;

myLevel:cleanUP()

This will remove the level and remove the LD_Helper reference.

LD_Loader creates a reference to LD_Helper and exposes the createObject function;

myLevel:createObject(layerName, objProps)

With this you can add objects to your level simply by passing in the correct properties as a table;

 local objProps = 
 {
    name = 'myNewOrange', 
    objType = 'LDImage',
    x = 250,
    y = 287,
    xScale = 10,
    yScale = 10,
    assetName = 'orange.png',
    physics = nil -- use asset physic properties
 }
local orange = myLevel:createObject("Layer1",objProps)

In this example it assumes that you have exported an asset called ‘orange.png’ and it will add it to Layer1. The createobject function will use the asset information to help create the object. Because we did not override the physics properties it will use any physics properties specified on the asset.  The coronaTemplate export will give you more information on what properties you can use.

If you need to access a layer you can get a local copy like this;

local layer1 = myLevel:getLayer(layerName)  -- add .view to access the displaygroup

To show/hide a layer;

myLevel:setLayerVisible(layerName, visible) -- visible = true/false

To retrieve a local copy of an object from a level you have two choices;
1) You do not know the layer name

local obj = myLevel:getObject(objectName).view

2) You know the layer name (best method)

local obj = myLevel:getLayerObject(layerName, objectName).view

If you are using class names on objects then you can get an array of objects with that class name;

local enemies = myLevel:objectsWithClass(className)

If they reside in a single layer then you can use this;

local enemies = myLevel:layerObjectsWithClass(layerName, className)

To remove an object;

myLevel:removeLayerObject(layerName,objectName)

To remove objects of a certain class on a known layer;

myLevel:removeLayerObjectsWithClass(layerName,className)

To remove objects of a certain class across all layers;

myLevel:removeObjectsWithClass(className)

LD_Helper

Here is where the main work is done when the level is loaded.

Although you don’t need to access LD_Helper directly you will access some functions indirectly via methods that get attached to objects.

Parallax Scrolling

if level.ParallaxEnabled is set then two methods are added to the level object;

myLevel:move( dx, dy )

This moves the objects by a delta amount and when level.ParallaxInfinite is set will move all the objects individually and wrap them accordingly.

myLevel:moveCamera(x ,y)

Move Camera will simply move the viewport/camera so the layer positions are offset by the camera position, no wrapping is performed, but the performance is much better.

Based on the ParallaxInfinite mode, when true objects will be moved otherwise when false whole layers are moved according to the speed set on the layer.

Note that you can only perform wrapping when ParallaxInfinite mode = true.

Also be aware that due to a limitation in Box2D, physics across multiple layers will not be moved if ParallaxInfinite = false, individual objects will have to be moved.

To add bounds checking when the layers are move call setCamerasBounds (The checking is based on the camera position which moves at a speed of 1.0) ;

myLevel:setCameraBounds(xMin, yMin, xMax, yMax)

To remove camera bounds;

myLevel:setCameraBounds(nil)

To obtain the camera position;

local cameraPos = myLevel:getCameraPos()
print (cameraPos.x, cameraPos.y)

To slide the level to a position use this;

myLevel:slideCameraToPosition(x, y, time, easing method, completed handler)

-- move to top corner 600,800
myLevel:slideCameraToPosition(600, 800, 2500, easing.inOutQuad, onScrollFinished)

To make the camera track a display object (see Scroller example);

myLevel:setCameraFocus(displayobject obj)  -- it is important for the layer the tracking object resides on to have a parallax speed of 1.0 otherwise you will get strange results.

-- Now start/stop the tracking
myLevel:trackEnabled(boolean enabled)

If you want to move the camera to a desired point without any visual movement use this;

myLevel:moveCamera(x, y)

Follow Paths

object:followPath(path, params)

If you have added paths to your level you can make any object follow that path by providing the name and the params table.

The params are as follows;

pathParams.rebound = int : 1 = rebound (switch direction) at end/start of path
pathParams.direction    = int : 1 = forward, -1 backwards
pathParams.delay        = int : bigger the number the slower it will be (0 = no delay).
pathParams.repeats      = int : -1 = forever, 0 = no repeat, n = repeat n times.
pathParams.rotate       = int : 1 = rotate according to angle between points
pathParams.xFlip        = bool : true = flip x axis at end/start of path
pathParams.yFlip        = bool : true = flip y axis at end/start of path
e.g. obj:followPath('path1', { direction=1, delay = 1.0})

To pause the path;

obj.pathParams.paused = true

To get a notification of when the path has reached the end/start two events are dispatched;

LD_Helper:instance().nPathEnded -- ("LD_PathEnded")
LD_Helper:instance().nPathObjectEnded --("LD_PathObjectEnded")

You can then listen to these events;

local pathEnded(event)
   print (event.object.name)
end
Runtime:addEventListener( LD_Helper:instance().nPathEnded, pathEnded) -- global listener 
obj:addEventListener( LD_Helper:instance().nPathObjectEnded,pathEnded) -- listen to individual objects

Misc

In addition to this some helper events are also added for you;

object.EnableDrag – if you set this to true on an object a touch event will be attached to the object allowing you to drag it around.

See the example projects for more information.