一つ言いたいことは、不要なオブジェクトを削除すると、メモリの問題が発生してアプリが遅くなるということです.しかし、lua言語にはメモリ管理があります。以下のコードが例外に達すると思います
local physics = require("physics")
physics.start()
local appletouchcount=0;local count={total1=0,total=0,touch=0,loss=0}
local total=display.newText("Total:0 \n AppleTotal:0 \n AppleGot:0 \n AppleLoss:0",display.contentCenterX*0.25, 60, native.systemFont, 26)
local collisionListener=function(self,event)
print(event.other.type)
if(event.phase=="began")then
if(event.other.type=="apple")then
count.loss=count.loss+1
event.other:removeSelf();event.other=nil
else
event.other:removeSelf();event.other=nil
end
end
end
function newApples(event)
count.total1=event.count
total.text="Total:"..count.total1.." \n AppleTotal:"..count.total.." \n AppleGot:"..count.touch.." \n AppleLoss:"..count.loss
rand = math.random( 100 )
if (rand < 60) then
j = display.newCircle(0,0,40)--display.newImage("s1.png");
j.x = 60 + math.random( 160 )
j.y = -100
j.type="other"
physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )
elseif (rand < 80) then
j = display.newCircle(0,0,40)--display.newImage("s2.png");
j.x = 60 + math.random( 160 )
j.y = -100
j.type="other"
physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )
else
apple =display.newCircle(0,0,70) --display.newImage("apple1.png");
apple.x = 60 + math.random( 160 )
apple.y = -100
apple.type="apple"
apple:setFillColor(255,0,0)
count.total= count.total+1
physics.addBody( apple, { density=0.3, friction=0.2, bounce=0.5} )
apple.name = "apple"
function onTouch(event)
count.touch=count.touch+1
total.text="Total:"..count.total1.." \n AppleTotal:"..count.total.." \n AppleGot:"..count.touch.." \n AppleLoss:"..count.loss
event.target:removeSelf()
print("total"..appletouchcount)
end
apple:addEventListener("touch",onTouch)
end
end
botwall=display.newRect(0,display.contentHeight,display.contentWidth,10)
botwall:setFillColor(22,125,185,255)
botwall.type="botwall"
botwall.collision=collisionListener
physics.addBody(botwall,"static",{ density=100.0, friction=0.0, bounce=0.0} )
botwall:addEventListener("collision",botwall)
local dropApples = timer.performWithDelay( 500, newApples, -1 )