Hello,
If it isn’t made immediately apparent I am fairly new to corona development and recently hit a roadblock in my development that I would greatly appreciate input/help with.
Basically, I want to get every object that is spawned on a timer to follow a set path. As it stands, each time I run the program, only the first object spawned will follow the path, and all subsequent objects will simply float in place. I tried to encase the function that sets the object on its path in an infinite “while” loop, but I received a memory error.
Here is the section of code in question, I apologize if it’s a little illegible -
[lua]
local spawnTimer
local spawnedObjects = {}
local spawnParams = {
xMin = 34,
xMax = 35,
yMin = 99,
yMax = 100,
spawnTime = 6000,
spawnOnTimer = 6000,
spawnInitial = 1
}
– Spawn an item
local function spawnItem( bounds )
– Create item
item = display.newCircle( 0, 0, 10 )
item:setFillColor( 1 )
– Position item within bounds
item.x = math.random( bounds.xMin, bounds.xMax )
item.y = math.random( bounds.yMin, bounds.yMax )
– Add item to spawnedObjects table to track it
spawnedObjects[#spawnedObjects+1] = item
end
– Spawn controller
local function spawnController( action, params )
– Cancel timer on “start” or “stop”, if it exists
if ( spawnTimer and ( action == “start” or action == “stop” ) ) then
timer.cancel( spawnTimer )
end
– Start spawning
if ( action == “start” ) then
– gather/set spawning bounds
local spawnBounds = {}
spawnBounds.xMin = params.xMin or 0
spawnBounds.xMax = params.xMax or display.contentWidth
spawnBounds.yMin = params.yMin or 0
spawnBounds.yMax = params.yMax or display.contentHeight
– gather/set other spawning params
local spawnTime = params.spawnTime or 1000
local spawnOnTimer = params.spawnOnTimer or 50
local spawnInitial = params.spawnInitial or 0
– if spawnInitial > 0, spawn n item(s) instantly
if ( spawnInitial > 0 ) then
for n = 1,spawnInitial do
spawnItem( spawnBounds )
end
end
– start repeating timer to spawn items
if ( spawnOnTimer > 0 ) then
spawnTimer = timer.performWithDelay( spawnTime,
function() spawnItem( spawnBounds ); end,
spawnOnTimer )
end
– Pause spawning
elseif ( action == “pause” ) then
timer.pause( spawnTimer )
– Resume spawning
elseif ( action == “resume” ) then
timer.resume( spawnTimer )
end
end
spawnController( “start”, spawnParams )
–spawnController( “pause” )
–spawnController( “resume” )
–spawnController( “stop” )
–Item movepath
local movePath = {}
movePath[1] = { x= 20, y= 30}
movePath[2] = { x= 40, y= 60}
movePath[3] = { x= 60, y= 90}
movePath[4] = { x= 80, y= 120}
movePath[5] = { x= 100, y= 150}
movePath[6] = { x= 120, y= 180}
movePath[7] = { x= 140, y= 210}
movePath[8] = { x= 160, y= 240}
–Distance Function
local function distBetween( x1, y1, x2, y2 )
local xFactor = x2 - x1
local yFactor = y2 - y1
local dist = math.sqrt( (xFactor*xFactor) + (yFactor*yFactor) )
return dist
end
–setPath function
local function setPath( object, path, params )
local delta = params.useDelta or nil
local deltaX = 0
local deltaY = 0
local constant = params.constantRate or nil
local ease = params.easingMethod or easing.linear
local tag = params.tag or nil
local delay = params.delay or 0
local speedFactor = 1
if ( delta ) then
deltaX = item.x
deltaY = item.y
end
if ( constant ) then
local dist = distBetween( object.x, object.y, deltaX+path[1].x, deltaY+path[1].y )
speedFactor = constant/dist
end
easingmethod = easing.outQuad
for i = 1,#path do
local segmentTime = 500
--if “constant” is defined, refactor transition time based on distance between points
if ( constant ) then
local dist
if ( i == 1 ) then
dist = distBetween( object.x, object.y, deltaX+path[i].x, deltaY+path[i].y )
else
dist = distBetween( path[i-1].x, path[i-1].y, path[i].x, path[i].y )
end
segmentTime = dist*speedFactor
else
--if this path segment has a custom time, use it
if ( path[i].time ) then segmentTime = path[i].time end
end
--if this segment has custom easing, override the default method (if any)
if ( path[i].easingMethod ) then ease = path[i].easingMethod end
transition.to( object, { tag=tag, time=segmentTime, x=deltaX+path[i].x, y=deltaY+path[i].y, delay=delay, transition=ease } )
delay = delay + segmentTime
end
end
setPath( item, movePath, { useDelta=true, constantTime=1200, easingMethod=easing.inOutQuad, delay=200, tag=“moveObject” } )
[/lua]
Once again, I greatly appreciate any help/input. Thanks in advance.