You’re a trooper for sticking with this, thanks a million.
For my code, I tried implementing your add and remove functions, and it seems like I’m plugging it in wrong. I assume that the issue I am having is that it’s giving all of the items the same name, and as such is only adding a single item to the table. How do I get it to name each item differently? (If that is the issue at all).
[code]
local score = 0
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )
local x = 50
local background = display.newImage(“background.png”)
scoreTextfield = display.newText( " " … score, 130, 285, nil, 14 )
scoreTextfield:setTextColor( 255, 0, 0, 255 )
scoreTextfield.text = " " … score
collectgarbage(“collect”)
score = "System Memory : " … collectgarbage(“count”)
scoreTextfield.text = " " … score
allBall = {}
local function removeByName( allBall, name )
for i,v in ipairs( allBall ) do
if ( v ~= nil and v == name ) then
allBall[i] = nil ; break
end
end
end
local function addByName( allBall, name )
local vCount = 0
for i,v in ipairs( allBall ) do
if ( v == name ) then
vCount = vCount+1
end
end
if ( vCount == 0 ) then
allBall[#allBall + 1] = name
end
end
local function randomBall ()
if x < 3000 then
local imageBall = display.newImage( “ball.png” )
allBall[#allBall + 1] = imageBall
local oneBall = allBall[#allBall]
addByName( allBall, oneBall )
local randomPos = math.random
oneBall.x = 10 + randomPos( 300 ); oneBall.y = -20
physics.addBody( oneBall, { density=2.9, friction=0.0, bounce = 0 } )
oneBall:setLinearVelocity( 0, 600 )
end
local function removeOffscreenItems()
for i = 1, #allBall do
local oneBall = allBall[i]
if ( oneBall.x ) then
if ( oneBall.y > display.contentHeight + 30 ) then
removeByName( allBall, oneBall )
oneBall:removeSelf()
oneBall = nil
y = y - 1
collectgarbage(“collect”)
score = "System Memory : " … collectgarbage(“count”)
scoreTextfield.text = " " … score
end
end
end
end
Runtime:addEventListener( “enterFrame”, removeOffscreenItems )
end
y = 30
local timberBall = timer.performWithDelay( x, randomBall, y )
[/code] [import]uid: 10903 topic_id: 3378 reply_id: 11946[/import]