I am slowly going mad I think. Thank you ever so much to Don and Larry for their advice to date it has been invaluable and have learnt so much. However, despite getting my code up to 60fps and reducing the total milliseconds required to calculate the effort I still get stuttering graphics on both simulator and device.
This is becoming a real problem for me with such a simple example. I have reduced my code by over 40% and think I have implemented all the optimisation tips correctly because as I said Im achieving 60 fps .
I have refactored the code to use two different methods. The second, using transition.to, isn’t fully implemented but enough for me to profile to see if it makes a difference and eradicate the stutter / pausing graphics.
The first is my optimised original code
[lua]local circles = {}
local stars = {}
local starsCached = {}
– initial vars
local stars_total = 300
local stars_field1= 100
local stars_field2= 200
local stars_field3= 400
local star_width = 1
local star_height = 1
lowerStarfield = 1.4
middleStarfield = 2
upperStarfield = 3
circleCount = 10
_H = display.contentHeight
_W = display.contentWidth
local myRand = math.random
– create/draw objects
for i = 1, stars_total do
local star = {}
local _X = myRand(_W)
local _Y = myRand(_H)
star.object = display.newRect(_X,_Y,star_width,star_height)
star.object.cx = _X
star.object.cy = _Y
if (i <= stars_field1) then
star.object:setFillColor(100,100,100)
elseif (i <= stars_field2 and i > stars_field1) then
star.object:setFillColor(155,155,155)
elseif(i <= stars_field3 and i > stars_field2) then
star.object:setFillColor(255,255,255)
end
stars[i] = star
star = nil
end
function updatePositioning(position, speed)
stars[position].object.cx = stars[position].object.cx - speed
stars[position].object:translate(-speed ,0)
if (stars[position].object.cx < 0) then
stars[position].object:translate(_W ,0)
stars[position].object.cx = stars[position].object.cx + _W
end
end
local gameLoop = function()
– Update the number of circles
if(#circles < circleCount) then
local _X = myRand(_W, _W*2)
local _Y = myRand(0, _H)
local circle = display.newImageRect(“circle.png”, 64, 64)
circle:translate(_X, _Y)
circle.cx = _X
circle.cy = _Y
circle.name = “middle”
circle.index = #circles + 1
circles[circle.index] = circle
circle = nil
end
– Update the number of circles
for i = #circles,1, -1 do
local circle = {}
circle = circles[i]
print(circle.cx)
circle.cx = circle.cx - middleStarfield
circle:translate(-middleStarfield,0)
if(circle.cx <= -20) then
circle.cx = myRand(_W, _W+200)
circle.cy = myRand(0, _H)
circle.x = circle.cx
circle.y = circle.cy
end
circles[i] = circle
circle = nil
end
for i = stars_total,1, -1 do
if (i <= stars_field1) then
updatePositioning(i, lowerStarfield)
elseif (i <= stars_field2 and i > stars_field1) then
updatePositioning(i, middleStarfield)
elseif(i <= stars_field3 and i > stars_field2) then
updatePositioning(i, upperStarfield)
end
end
end
Runtime:addEventListener( “enterFrame” , gameLoop)[/lua]
The second is using transition.to to see if the lack of intervention makes a difference
[lua]local circles = {}
local stars = {}
local starsCached = {}
– initial vars
local stars_total = 500
local stars_field1= 200
local stars_field2= 400
local stars_field3= 800
local star_width = 1
local star_height = 1
lowerStarfield = 1.4
middleStarfield = 2
upperStarfield = 3
circleCount = 10
_H = display.contentHeight
_W = display.contentWidth
local myRand = math.random
local delta = 10000 / 480
– create/draw objects
local listener2= function( obj )
local _X = myRand(_W, _W*2)
local _Y = myRand(0, _H)
local milli = (_X -480) * delta
obj.y = _Y
obj.x = _X
transition.to( obj, { time=(_X * delta)* 1.2, x=-30, onComplete=listener2 } )
end
local listener3= function( obj )
local _X = myRand(_W, _W*2)
local _Y = myRand(0, _H)
local milli = (_X -480) * delta
obj.y = _Y
obj.x = _X
transition.to( obj, { time=(_X * delta)* 0.9, x=-30, onComplete=listener3 } )
end
local listener4= function( obj )
local _X = myRand(_W, _W*2)
local _Y = myRand(0, _H)
local milli = (_X -480) * delta
obj.y = _Y
obj.x = _X
transition.to( obj, { time=(_X * delta) * 0.7, x=-30, onComplete=listener4 } )
end
local listener1 = function( obj )
local _X = myRand(_W, _W*2)
local milli = (_X -480) * delta
local _Y = myRand(0, _H)
obj.y = _Y
obj.x = _X
transition.to( obj, { time=(10000 + milli), x=-30, onComplete=listener1 } )
end
for i = 1, stars_total do
local star = {}
local _X = myRand(_W)
local _Y = myRand(_H)
local milli = (_X -480) * delta
star.object = display.newRect(_X,_Y,star_width,star_height)
star.object.cx = _X
star.object.cy = _Y
if (i <= stars_field1) then
star.object:setFillColor(100,100,100)
transition.to( star.object, { time=(_X * delta)* 1.2, x=-30, onComplete=listener2 } )
elseif (i <= stars_field2 and i > stars_field1) then
star.object:setFillColor(155,155,155)
transition.to( star.object, { time=(_X * delta)* 0.9, x=-30, onComplete=listener3 } )
elseif(i <= stars_field3 and i > stars_field2) then
star.object:setFillColor(255,255,255)
transition.to( star.object, { time=(_X * delta) * 0.7, x=-30, onComplete=listener4 } )
end
stars[i] = star
star = nil
end
local gameLoop = function()
– Update the number of circles
if(#circles < circleCount) then
local _X = myRand(_W, _W*2)
local milli = (_X -480) * delta
local _Y = myRand(0, _H)
local circle = display.newImageRect(“circle.png”, 64, 64)
–circle:translate(_X, _Y)
circle.x = _X
circle.y = _Y
transition.to( circle, { time=(10000 + milli), x=-30, onComplete=listener1 } )
circle.name = “middle”
circle.index = #circles + 1
circles[circle.index] = circle
circle = nil
end
– Update the number of circles
end
Runtime:addEventListener( “enterFrame” , gameLoop)[/lua]
I am using the incredibly useful Corona Profiler by M.Y. Developers to analyse hot spots which I think I have but the performance just doesn’t seem to be there. Any help or advice would be greatly appreciated and I’m sure invaluable to any newbie like me who is delving into the complex world of optimisation.
J
P.S. free advice please as Don no I can’t afford your expertise
[import]uid: 103970 topic_id: 21203 reply_id: 84693[/import]