Hide a bit of lag using "newTrail" function

Hi guys!

In my app I use the function “newTrail”. It works well but in some cases it creates a bit of lag.

For example if I’m uploading advertising or doing other runtime checks.

I know that I should load the advertisement in scenes not animated but in this case I am forced.

I am attaching the code and what I get in the two cases:

code:

local myImage = display.newImageRect( "myImage.png", 30, 30 ) myImage:translate( 160, 20 ) transition.to( myImage, {time=5000, y=460, iterations=-1, transition=easing.continuousLoop}) local function lenSqr( dx, dy, dz ) return ( dx \* dx + dy \* dy + dz \* dz ) end local function newTrail( object, options ) if not object.contentBounds then print( "WARNING: Object not found" ) return false end options = options or {} local image = options.image or "myImage.png" local dw, dh = object.width, object.height local size = options.size or ( dw \> dh ) and ( dw \* 0.9 ) or ( dh \* 0.9 ) local w, h = size, size local ox, oy = options.offsetX or 0, options.offsetY or 0 local trans = options.transition or { time = 250, alpha = 0, delay = 50, xScale = 0.01, yScale = 0.01 } local delay = options.delay or 0 local color = options.color or { 1.0 } local alpha = options.alpha or 0.5 local blendMode = options.blendMode or "add" local frameSkip = options.frameSkip or 1 local frame = 1 local trail = display.newGroup() --se vi è un diverso punto di ancoraggio eseguo dei controlli per esso if((object.anchorX ~= .5) or (object.anchorY ~= .5))then --creo un immagine solo per il giusto posizonamento del ancoraggio trail.tmp = display.newRect(trail, 0, 0, object.width, object.height) trail.tmp.isVisible = false trail.anchorChildren = true trail.anchorX = object.anchorX trail.anchorY = object.anchorY end if options.parent then options.parent:insert( trail ) else if object.parent then object.parent:insert( trail ) end end trail.ox, trail.oy, trail.oz, trail.oa = object.x, object.y, ( object.z or 0 ), object.rotation trail.alpha = alpha local function enterFrame() frame = frame + 1 -- Object destroyed if not object.contentBounds then trail:finalize() return false end -- Haven't moved if lenSqr( object.x - trail.ox, object.y - trail.oy, ( object.z or 0 ) - trail.oz ) \< 1 \* 1 then return false end trail.ox, trail.oy, trail.oz = object.x, object.y, (object.z or 0) if frame \> frameSkip then frame = 1 else return false end -- Create trail local particle = display.newImageRect( trail, image, w, h ) transition.from( particle, { alpha = 0, time = delay } ) -- Color particle:setFillColor( unpack(color) ) particle.blendMode = blendMode -- Place particle.x, particle.y = object.x + ox, object.y + oy - ( object.z or 0 ) particle.rotation = object.rotation -- Finalization trans.onComplete = function() display.remove( particle ) particle = nil end -- Transition transition.to( particle, trans ) end Runtime:addEventListener("enterFrame", enterFrame) function trail:finalize() Runtime:removeEventListener( "enterFrame", enterFrame ) local function onComplete() display.remove( self ) trail = nil end transition.to( trail, {alpha = 0, onComplete = onComplete } ) end trail:addEventListener( "finalize" ) return trail end newTrail( myImage, { frameSkip = 5, transition = { time = 1000, alpha = 0, delay = 50, xScale = 0.01, yScale = 0.01 } } )

image:

My question is there a way to avoid this?

for example, I would prefer that the main ball momentarily block rather than ruining the trail.

Or anything else

Thanks in advance…

I wrote the original version of that function  :slight_smile:

The system is going to lag when you do stuff in the background. It’s just how it is–that said, if you want to speed the whole function up, remove all the object.z stuff (you won’t need it) or use corona particles to make the trail.

Good luck!

Hello thanks for the advice (and for the newTrail code)!

I followed your advice and created a smoother function:

function M.newTrail2( object, options ) if not object.contentBounds then print( "WARNING: Object not found" ) return false end local image = options.image local color = options.color or { 1.0 } local frameSkip = options.frameSkip or 1 local trans = options.transition or { time = 250, alpha = 0, delay = 50, xScale = 0.01, yScale = 0.01 } local dim\_W = options.width or object.width local dim\_H = options.height or object.height local alpha = options.alpha or 0.5 local tag = "trail"..tostring(options) local blendMode local relativeCoordinates = options.relativeCoordinates if(options.blendMode == nil)then blendMode = "add" elseif(options.blendMode == false)then blendMode = nil else blendMode = options.blendMode end local masterG = display.newGroup( ) if(options.parent)then options.parent:insert(masterG) end object:toFront( ) local frame = 1 local function scia() if not object.contentBounds then masterG:finalize() return false end frame = frame+1 if frame \> frameSkip then frame = 1 else return false end local particle = display.newImageRect( masterG, image, dim\_W, dim\_H ) particle.alpha = alpha particle.isVisible = object.isVisible --transition.to( particle, { time = 4000, alpha = 0 } ) if(relativeCoordinates)then particle:translate( object.x, object.y ) else particle:translate( object:localToContent( 0, 0 ) ) end particle:rotate( object.rotation ) -- Color particle:setFillColor( unpack(color) ) particle.blendMode = blendMode trans.onComplete = function() display.remove( particle ) particle = nil end trans.tag = tag -- Transition transition.to( particle, trans ) end Runtime:addEventListener( "enterFrame", scia ) function masterG:finalize() Runtime:removeEventListener( "enterFrame", scia ) transition.cancel(tag) local function onComplete() display.remove( self ) masterG = nil end transition.to( masterG, {alpha = 0, onComplete = onComplete } ) end masterG:addEventListener( "finalize" ) return masterG end

This has improved but the problem is still present.

What else could I do to improve?

Try to find a better time to pre-load your ads  :wink:

I wrote the original version of that function  :slight_smile:

The system is going to lag when you do stuff in the background. It’s just how it is–that said, if you want to speed the whole function up, remove all the object.z stuff (you won’t need it) or use corona particles to make the trail.

Good luck!

Hello thanks for the advice (and for the newTrail code)!

I followed your advice and created a smoother function:

function M.newTrail2( object, options ) if not object.contentBounds then print( "WARNING: Object not found" ) return false end local image = options.image local color = options.color or { 1.0 } local frameSkip = options.frameSkip or 1 local trans = options.transition or { time = 250, alpha = 0, delay = 50, xScale = 0.01, yScale = 0.01 } local dim\_W = options.width or object.width local dim\_H = options.height or object.height local alpha = options.alpha or 0.5 local tag = "trail"..tostring(options) local blendMode local relativeCoordinates = options.relativeCoordinates if(options.blendMode == nil)then blendMode = "add" elseif(options.blendMode == false)then blendMode = nil else blendMode = options.blendMode end local masterG = display.newGroup( ) if(options.parent)then options.parent:insert(masterG) end object:toFront( ) local frame = 1 local function scia() if not object.contentBounds then masterG:finalize() return false end frame = frame+1 if frame \> frameSkip then frame = 1 else return false end local particle = display.newImageRect( masterG, image, dim\_W, dim\_H ) particle.alpha = alpha particle.isVisible = object.isVisible --transition.to( particle, { time = 4000, alpha = 0 } ) if(relativeCoordinates)then particle:translate( object.x, object.y ) else particle:translate( object:localToContent( 0, 0 ) ) end particle:rotate( object.rotation ) -- Color particle:setFillColor( unpack(color) ) particle.blendMode = blendMode trans.onComplete = function() display.remove( particle ) particle = nil end trans.tag = tag -- Transition transition.to( particle, trans ) end Runtime:addEventListener( "enterFrame", scia ) function masterG:finalize() Runtime:removeEventListener( "enterFrame", scia ) transition.cancel(tag) local function onComplete() display.remove( self ) masterG = nil end transition.to( masterG, {alpha = 0, onComplete = onComplete } ) end masterG:addEventListener( "finalize" ) return masterG end

This has improved but the problem is still present.

What else could I do to improve?

Try to find a better time to pre-load your ads  :wink: