I am trying to develop a game in which balls will continue rolling downward. But the game starts hanging up after few seconds.
I am attaching the code , please help me out.
I am trying to develop a game in which balls will continue rolling downward. But the game starts hanging up after few seconds.
I am attaching the code , please help me out.
Hey there,
in spawnnewloop you are checking for #myTable to be less than 200 as a condition for spawnnew() to execute. If you print out the value of #myTable you’ll notice that at the time the app “blocks” it will be 200.
The idea is that you are removing from a table by setting to nil. You should use table.remove:
https://www.lua.org/pil/19.2.html
In which case your table count ( #myTable ) will decrease correctly when removing from it.
Hope this helps,
alex
Thanks for your reply. This is still giving us problems , we have added table.remove(myTable,s) but the game still hangs.
Save the Dot Retry 3.zip In this we are removing the objjects that are going down and adding new objects at the top. When one balls goes down there are 5 new balls are generated at top.
for j=#myTable,1,-1 do
if(myTable[j]~=nil) then
local function spawnnewrings(event)
for s=#myTable,1,-1 do
if myTable[s]~=nil then
if myTable[s].y>1135 then
– myTable[s].y=myTable[s].y-1160-1024
transition.cancel( myTable[s] )
table.remove(myTable,s)
–myTable[s].kill=true
display.remove(myTable[s])
myTable[s] = nil
end
end
end
if j==#myTable then
spawnnew()
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local physics = require "physics" physics.start() physics.setGravity( 0, 0) local math = require("math") display.setStatusBar(display.HiddenStatusBar) local outermainarray={} local h2=820; local dotsforshootarray = {} local bulletanglearray={} local dotarray={} local pausedcircle11 local pausedcircle local id=1; local DX, DY = 3, 3 local XMIN, XMAX = 1, display.contentWidth-1 local YMIN, YMAX = 20, display.contentHeight-1 \_W = display.contentWidth; -- Get the width of the screen \_H = display.contentHeight; -- Get the height of the screen motionx = 0; -- Variable used to move character along x axis speed = 2; -- Set Walking Speed local w = display.contentWidth local h = display.contentHeight local centerX = display.contentCenterX local centerY = display.contentCenterY local myTable = {} local rect local Cos = math.cos local Sin = math.sin local Rad = math.rad local Atan2 = math.atan2 local Deg = math.deg local function spawnnewloop( event ) for j=#myTable,1,-1 do if(myTable[j]~=nil) then local function spawnnewrings(event) for s=#myTable,1,-1 do if myTable[s]~=nil then if myTable[s].y\>1135 then -- myTable[s].y=myTable[s].y-1160-1024 transition.cancel( myTable[s] ) table.remove(myTable,s) --myTable[s].kill=true display.remove(myTable[s]) myTable[s] = nil end end end if j==#myTable then spawnnew() end end transition.to(myTable[j],{time=2000, y=myTable[j].y+1200,onComplete=spawnnewrings}) end end end local id=1 function spawnBubble(ringx,ringy,ringradi,ringstart,ringend,bubblex,bubbley,rotationspeed) -- add bottom arch 2 local circle2bubble =display.newCircle(display.contentWidth/2,display.contentHeight/2,25) circle2bubble:setFillColor( 0, 1, 1 ) circle2bubble.id=id id=id+1 circle2bubble.x = bubblex circle2bubble.y = bubbley-364 circle2bubble.linearDamping=0 circle2bubble.myName="bubble" physics.addBody(circle2bubble) transition.to(circle2bubble,{time=1000, y=bubbley,x=bubblex}) circle2bubble.isSensor = true myTable[#myTable+1]=circle2bubble transition.to(circle2,{time=1000, y=ringy,x=ringx}) end function spawnnew( event ) if (myTable[#myTable]~=nil) then h2=myTable[#myTable].y-200 end for m = 1, 10 do if(m%5==1) then spawnBubble(384,h2, 2, -95,2,384,h2,6.5) end if(m%5==2) then spawnBubble(160,h2, 2, -95,2,160,h2,6.5) end if(m%5==3) then spawnBubble(520, h2, 2, -95,2,520,h2,6.5) end if(m%5==4) then spawnBubble(190, h2, 2, -95,2,190,h2,6.5) end if(m%5==0) then spawnBubble(500, h2, 2, -95,2,500,h2,6.5) --h2=h2+700 end h2=h2-200 spawnnewloop() end print(h2) end spawnnew()
Save the Dot Retry 4.zip : This code gave us better results
We also tried something else , which is in the code below ,
if myTable[s]~=nil then
if myTable[s].y>1135 then
myTable[s].y=myTable[s].y-1160-1024 , By adding this we are not generating new objects at the top but using the objects again at the top that have scrolled down.
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local physics = require "physics" physics.start() physics.setGravity( 0, 0) local math = require("math") display.setStatusBar(display.HiddenStatusBar) local outermainarray={} local h2=820; local dotsforshootarray = {} local bulletanglearray={} local dotarray={} local pausedcircle11 local pausedcircle local id=1; local DX, DY = 3, 3 local XMIN, XMAX = 1, display.contentWidth-1 local YMIN, YMAX = 20, display.contentHeight-1 \_W = display.contentWidth; -- Get the width of the screen \_H = display.contentHeight; -- Get the height of the screen motionx = 0; -- Variable used to move character along x axis speed = 2; -- Set Walking Speed local w = display.contentWidth local h = display.contentHeight local centerX = display.contentCenterX local centerY = display.contentCenterY local myTable = {} local rect local Cos = math.cos local Sin = math.sin local Rad = math.rad local Atan2 = math.atan2 local Deg = math.deg local function spawnnewloop( event ) for j=#myTable,1,-1 do if(myTable[j]~=nil) then local function spawnnewrings(event) for s=#myTable,1,-1 do if myTable[s]~=nil then if myTable[s].y\>1135 then myTable[s].y=myTable[s].y-1160-1024 -- transition.cancel( myTable[s] ) -- table.remove(myTable,s) -- -- --myTable[s].kill=true -- display.remove(myTable[s]) -- myTable[s] = nil end end end if j==#myTable then spawnnewloop() end end transition.to(myTable[j],{time=2000, y=myTable[j].y+1200,onComplete=spawnnewrings}) end end end local id=1 function spawnBubble(ringx,ringy,ringradi,ringstart,ringend,bubblex,bubbley,rotationspeed) -- add bottom arch 2 local circle2bubble =display.newCircle(display.contentWidth/2,display.contentHeight/2,25) circle2bubble:setFillColor( 0, 1, 1 ) circle2bubble.id=id id=id+1 circle2bubble.x = bubblex circle2bubble.y = bubbley-364 circle2bubble.linearDamping=0 circle2bubble.myName="bubble" physics.addBody(circle2bubble) transition.to(circle2bubble,{time=1000, y=bubbley,x=bubblex}) circle2bubble.isSensor = true myTable[#myTable+1]=circle2bubble transition.to(circle2,{time=1000, y=ringy,x=ringx}) end function spawnnew( event ) if (myTable[#myTable]~=nil) then h2=myTable[#myTable].y-200 end for m = 1, 10 do if(m%5==1) then spawnBubble(384,h2, 2, -95,2,384,h2,6.5) end if(m%5==2) then spawnBubble(160,h2, 2, -95,2,160,h2,6.5) end if(m%5==3) then spawnBubble(520, h2, 2, -95,2,520,h2,6.5) end if(m%5==4) then spawnBubble(190, h2, 2, -95,2,190,h2,6.5) end if(m%5==0) then spawnBubble(500, h2, 2, -95,2,500,h2,6.5) --h2=h2+700 end h2=h2-200 spawnnewloop() end print(h2) end spawnnew()
Somebody please help
Hey there,
in spawnnewloop you are checking for #myTable to be less than 200 as a condition for spawnnew() to execute. If you print out the value of #myTable you’ll notice that at the time the app “blocks” it will be 200.
The idea is that you are removing from a table by setting to nil. You should use table.remove:
https://www.lua.org/pil/19.2.html
In which case your table count ( #myTable ) will decrease correctly when removing from it.
Hope this helps,
alex
Thanks for your reply. This is still giving us problems , we have added table.remove(myTable,s) but the game still hangs.
Save the Dot Retry 3.zip In this we are removing the objjects that are going down and adding new objects at the top. When one balls goes down there are 5 new balls are generated at top.
for j=#myTable,1,-1 do
if(myTable[j]~=nil) then
local function spawnnewrings(event)
for s=#myTable,1,-1 do
if myTable[s]~=nil then
if myTable[s].y>1135 then
– myTable[s].y=myTable[s].y-1160-1024
transition.cancel( myTable[s] )
table.remove(myTable,s)
–myTable[s].kill=true
display.remove(myTable[s])
myTable[s] = nil
end
end
end
if j==#myTable then
spawnnew()
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local physics = require "physics" physics.start() physics.setGravity( 0, 0) local math = require("math") display.setStatusBar(display.HiddenStatusBar) local outermainarray={} local h2=820; local dotsforshootarray = {} local bulletanglearray={} local dotarray={} local pausedcircle11 local pausedcircle local id=1; local DX, DY = 3, 3 local XMIN, XMAX = 1, display.contentWidth-1 local YMIN, YMAX = 20, display.contentHeight-1 \_W = display.contentWidth; -- Get the width of the screen \_H = display.contentHeight; -- Get the height of the screen motionx = 0; -- Variable used to move character along x axis speed = 2; -- Set Walking Speed local w = display.contentWidth local h = display.contentHeight local centerX = display.contentCenterX local centerY = display.contentCenterY local myTable = {} local rect local Cos = math.cos local Sin = math.sin local Rad = math.rad local Atan2 = math.atan2 local Deg = math.deg local function spawnnewloop( event ) for j=#myTable,1,-1 do if(myTable[j]~=nil) then local function spawnnewrings(event) for s=#myTable,1,-1 do if myTable[s]~=nil then if myTable[s].y\>1135 then -- myTable[s].y=myTable[s].y-1160-1024 transition.cancel( myTable[s] ) table.remove(myTable,s) --myTable[s].kill=true display.remove(myTable[s]) myTable[s] = nil end end end if j==#myTable then spawnnew() end end transition.to(myTable[j],{time=2000, y=myTable[j].y+1200,onComplete=spawnnewrings}) end end end local id=1 function spawnBubble(ringx,ringy,ringradi,ringstart,ringend,bubblex,bubbley,rotationspeed) -- add bottom arch 2 local circle2bubble =display.newCircle(display.contentWidth/2,display.contentHeight/2,25) circle2bubble:setFillColor( 0, 1, 1 ) circle2bubble.id=id id=id+1 circle2bubble.x = bubblex circle2bubble.y = bubbley-364 circle2bubble.linearDamping=0 circle2bubble.myName="bubble" physics.addBody(circle2bubble) transition.to(circle2bubble,{time=1000, y=bubbley,x=bubblex}) circle2bubble.isSensor = true myTable[#myTable+1]=circle2bubble transition.to(circle2,{time=1000, y=ringy,x=ringx}) end function spawnnew( event ) if (myTable[#myTable]~=nil) then h2=myTable[#myTable].y-200 end for m = 1, 10 do if(m%5==1) then spawnBubble(384,h2, 2, -95,2,384,h2,6.5) end if(m%5==2) then spawnBubble(160,h2, 2, -95,2,160,h2,6.5) end if(m%5==3) then spawnBubble(520, h2, 2, -95,2,520,h2,6.5) end if(m%5==4) then spawnBubble(190, h2, 2, -95,2,190,h2,6.5) end if(m%5==0) then spawnBubble(500, h2, 2, -95,2,500,h2,6.5) --h2=h2+700 end h2=h2-200 spawnnewloop() end print(h2) end spawnnew()
Save the Dot Retry 4.zip : This code gave us better results
We also tried something else , which is in the code below ,
if myTable[s]~=nil then
if myTable[s].y>1135 then
myTable[s].y=myTable[s].y-1160-1024 , By adding this we are not generating new objects at the top but using the objects again at the top that have scrolled down.
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local physics = require "physics" physics.start() physics.setGravity( 0, 0) local math = require("math") display.setStatusBar(display.HiddenStatusBar) local outermainarray={} local h2=820; local dotsforshootarray = {} local bulletanglearray={} local dotarray={} local pausedcircle11 local pausedcircle local id=1; local DX, DY = 3, 3 local XMIN, XMAX = 1, display.contentWidth-1 local YMIN, YMAX = 20, display.contentHeight-1 \_W = display.contentWidth; -- Get the width of the screen \_H = display.contentHeight; -- Get the height of the screen motionx = 0; -- Variable used to move character along x axis speed = 2; -- Set Walking Speed local w = display.contentWidth local h = display.contentHeight local centerX = display.contentCenterX local centerY = display.contentCenterY local myTable = {} local rect local Cos = math.cos local Sin = math.sin local Rad = math.rad local Atan2 = math.atan2 local Deg = math.deg local function spawnnewloop( event ) for j=#myTable,1,-1 do if(myTable[j]~=nil) then local function spawnnewrings(event) for s=#myTable,1,-1 do if myTable[s]~=nil then if myTable[s].y\>1135 then myTable[s].y=myTable[s].y-1160-1024 -- transition.cancel( myTable[s] ) -- table.remove(myTable,s) -- -- --myTable[s].kill=true -- display.remove(myTable[s]) -- myTable[s] = nil end end end if j==#myTable then spawnnewloop() end end transition.to(myTable[j],{time=2000, y=myTable[j].y+1200,onComplete=spawnnewrings}) end end end local id=1 function spawnBubble(ringx,ringy,ringradi,ringstart,ringend,bubblex,bubbley,rotationspeed) -- add bottom arch 2 local circle2bubble =display.newCircle(display.contentWidth/2,display.contentHeight/2,25) circle2bubble:setFillColor( 0, 1, 1 ) circle2bubble.id=id id=id+1 circle2bubble.x = bubblex circle2bubble.y = bubbley-364 circle2bubble.linearDamping=0 circle2bubble.myName="bubble" physics.addBody(circle2bubble) transition.to(circle2bubble,{time=1000, y=bubbley,x=bubblex}) circle2bubble.isSensor = true myTable[#myTable+1]=circle2bubble transition.to(circle2,{time=1000, y=ringy,x=ringx}) end function spawnnew( event ) if (myTable[#myTable]~=nil) then h2=myTable[#myTable].y-200 end for m = 1, 10 do if(m%5==1) then spawnBubble(384,h2, 2, -95,2,384,h2,6.5) end if(m%5==2) then spawnBubble(160,h2, 2, -95,2,160,h2,6.5) end if(m%5==3) then spawnBubble(520, h2, 2, -95,2,520,h2,6.5) end if(m%5==4) then spawnBubble(190, h2, 2, -95,2,190,h2,6.5) end if(m%5==0) then spawnBubble(500, h2, 2, -95,2,500,h2,6.5) --h2=h2+700 end h2=h2-200 spawnnewloop() end print(h2) end spawnnew()
Somebody please help