Hi all on my second day of learning Lua and corona. Trying to get to grips with some OOP and need a little help trying to resolve a issue I cant seem to suss.
I have created this module class for a scrolling back ground which is working correctly apart from I can not work out how to “Return” the heightTravelled variable to my calling script, here the class:
function new(imageSelection)
local speed = 0.02
heightTravelled = 0
local sky = display.newImage(imageSelection)
sky:setReferencePoint(display.CenterLeftReferencePoint)
sky.x = 0
sky.y = 0
local sky2 = display.newImage(imageSelection)
sky2:setReferencePoint(display.CenterLeftReferencePoint)
sky2.x = 0
sky2.y = 480
function sky:velocity(value)
speed = value
end
function sky:distance()
return heightTravlled
end
local function addHeight()
heightTravelled = heightTravelled + 480
print(heightTravelled)
end
local tPrevious = system.getTimer()
local function move(event)
local tDelta = event.time - tPrevious
tPrevious = event.time
local yOffset = ( speed \* tDelta )
sky.y = sky.y + yOffset
sky2.y = sky2.y + yOffset
if (sky.y + sky.contentHeight-240) \> 960 then
sky:translate( 0, -960)
addHeight()
end
if (sky2.y + sky2.contentHeight-240) \> 960 then
sky2:translate(0, -960)
addHeight()
end
end
Runtime:addEventListener( "enterFrame", move )
return sky, heightTravlled
end
Here is a snippet from my calling script as you can see I have worked out how to pass variables to functions in the class just cant seem to get a value returned.
[code] – call Module to creat scrolling sky
local scrollingSky = ScrollingDown.new(“basicCloudSky.png”)
scrollingSky:velocity(0.10)
– Would like to get value returned e.g. something like but it does not work
h = scrollingSky:distance()
[/code]
Much appreciated [import]uid: 118379 topic_id: 20869 reply_id: 320869[/import]
