Here’s a sample code, if you run on the last version of Codea it’s smooth, but on 2.0 it’s choppy …
The reason there’s 4 blocks of for loops is that the original code also does destroys, etc.,
but basically it used to run great and now it doesn’t and I can’t pinpoint the issue …
I’m using setContext to draw paths already taken by the balls, and switch the context inside for loops.
Any Advice would be appreciated!
function setup()
supportedOrientations(LANDSCAPE_LEFT)
displayMode(FULLSCREEN)
backingMode(STANDARD)
walls = {}
balls = {}
cell = 40
createLevel()
end
function createLevel()
myImage = nil
myImage = image(WIDTH,HEIGHT)
setContext(myImage)
fill(42, 31, 106, 255)
rect(0,0,WIDTH,HEIGHT)
setContext()
for y = 0, 18 do
for x = 0, 20 do
if x==0 or x==2 or x==4 or x==16 or x==18 or x==20 or y==0 or y==18 then
createBox(x*cell,y*cell,cell,cell)
end
end
end
createCircle(cell*1,cell*17,cell/2,1)
createCircle(cell*3,cell*17,cell/2,2)
createCircle(cell*17,cell*17,cell/2,3)
createCircle(cell*19,cell*17,cell/2,4)
end
function createCircle(x,y,r,i)
local circle = physics.body(CIRCLE, r)
circle.type = DYNAMIC
circle.x = x
circle.y = y
circle.restitution = 0
circle.mass = 0
circle.friction = 0
circle.interpolate = true
circle.sleepingAllowed = false
circle.info = i
table.insert(balls,circle)
end
function createBox(x,y,w,h)
local box = physics.body(POLYGON, vec2(-w/2,h/2), vec2(-w/2,-h/2), vec2(w/2,-h/2), vec2(w/2,h/2))
box.type = STATIC
box.x = x
box.y = y
box.restitution = 0
box.mass = 0
box.friction = 0
box.interpolate = false
box.sleepingAllowed = true
table.insert(walls,box)
end
function draw()
sprite(myImage, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
for i,body in ipairs(balls) do
if body.info == 1 then
body.linearVelocity = vec2(Gravx,Gravy)
setContext(myImage)
fill(60, 140, 0, 255)
ellipse(body.x+body.radius,body.y+body.radius,body.radius*1.5)
setContext()
fill(120, 200, 60, 255)
ellipse(body.x+body.radius,body.y+body.radius,body.radius*2)
fill(255, 255, 255, 255)
ellipse(body.x+body.radius,body.y+body.radius,cell/2)
end
end
for i,body in ipairs(balls) do
if body.info == 2 then
body.linearVelocity = vec2(Gravx,Gravy)
setContext(myImage)
fill(140, 0, 140, 255)
ellipse(body.x+body.radius,body.y+body.radius,body.radius*1.5)
setContext()
fill(200, 60, 200, 255)
ellipse(body.x+body.radius,body.y+body.radius,body.radius*2)
fill(255, 255, 255, 255)
ellipse(body.x+body.radius,body.y+body.radius,cell/2)
end
end
for i,body in ipairs(balls) do
if body.info == 3 then
body.linearVelocity = vec2(Gravx,Gravy)
setContext(myImage)
fill(0, 100, 160, 255)
ellipse(body.x+body.radius,body.y+body.radius,body.radius*1.5)
setContext()
fill(60, 160, 220, 255)
ellipse(body.x+body.radius,body.y+body.radius,body.radius*2)
fill(255, 255, 255, 255)
ellipse(body.x+body.radius,body.y+body.radius,cell/2)
end
end
for i,body in ipairs(balls) do
if body.info == 4 then
body.linearVelocity = vec2(Gravx,Gravy)
setContext(myImage)
fill(180, 0, 0, 255)
ellipse(body.x+body.radius,body.y+body.radius,body.radius*1.5)
setContext()
fill(240, 60, 60, 255)
ellipse(body.x+body.radius,body.y+body.radius,body.radius*2)
fill(255, 255, 255, 255)
ellipse(body.x+body.radius,body.y+body.radius,cell/2)
end
end
Gravx = math.ceil(Gravity.x*5000)
Gravy = math.ceil(Gravity.y*5000)
if Gravx > 200 then Gravx = 200 end
if Gravx < -200 then Gravx = -200 end
if Gravy > 200 then Gravy = 200 end
if Gravy < -200 then Gravy = -200 end
for i,body in ipairs(walls) do
fill(29, 155, 213, 100)
noStroke()
rect(body.x,body.y,cell,cell)
end
end