Removing A Display Object Through A Table

Whatsup community

Ive been playing around trying to imitate a background i saw 96062033_1190182.gif?4 (but turn it into a simple game). Then i came across a problem where i couldnt remove the fallings objects through a table. Hmmmmmm

display.setStatusBar( display.HiddenStatusBar ) ----------------------------------------------------------------------------------------- -- Localize to improve performance ----------------------------------------------------------------------------------------- local W = display.contentWidth local H = display.contentHeight local XCentre = display.contentCenterX local YCentre = display.contentCenterY local screenLeft = display.screenOriginX local screenRight = display.viewableContentWidth + display.screenOriginX local screenTop = display.screenOriginY local screenBottom = display.viewableContentHeight + display.screenOriginY ---------------------------------------------------------------------------------------- -- Start and manipulate physics to suit environment/ game style ---------------------------------------------------------------------------------------- local physics = require("physics") local heroSpeed = 200 local random = math.random physics.start() physics.setGravity( 0, 0 ) ---------------------------------------------------------------------------------------- -- Add a bit of colour because things were looking boring ! ---------------------------------------------------------------------------------------- local BG = display.setDefault( "background", 0,1,1 ) ---------------------------------------------------------------------------------------- -- create table to hold our badguys! ---------------------------------------------------------------------------------------- local BG = {} local BadGuyNumb = 0 ---------------------------------------------------------------------------------------- -- Display, add Physics and insert BadGuys into a table ---------------------------------------------------------------------------------------- local function SpawnBadGuys ()     local Size = random (20,90)     local BadGuy = display.newRect(random(screenLeft,screenRight), 0, Size, Size )     table.insert( BG, BadGuy)     physics.addBody( BadGuy)     BadGuy.isSensor = true     transition.to( BadGuy,{time = random (1000,3000), y = screenBottom --[[+ Size \* .5]], onComplete = Destroy\_BadGuy} )     return BadGuy end local function Destroy\_BadGuy ()     BadGuyNumb = BadGuyNumb + 1     display.remove( BG[BadGuyNumb] )     BG[BadGuyNumb] = nil end timer.performWithDelay( random (120,500), SpawnBadGuys , -1)  

Thanks a bunch (and hello from england!) 

#1: You have the BG variable set to two different objects. 

#2: You need to have your function that you are calling, declared ahead of where you are calling it.

#3: You’re better off using a (obj) call on your removal function when your transition is completed.

I updated some of your code. Try it out now:

local BG1 = {} local BadGuyNumb = 0 ---------------------------------------------------------------------------------------- -- Display, add Physics and insert BadGuys into a table ---------------------------------------------------------------------------------------- local function Destroy\_BadGuy (obj) print("obj remove") display.remove( obj ) obj = nil end local function SpawnBadGuys () local Size = math.random(20,90) local BadGuy = display.newRect(0,0,40,40 ) table.insert( BG1, BadGuy) physics.addBody( BadGuy) BadGuy.isSensor = true transition.to( BadGuy,{time = math.random(1000,3000), y = screenBottom --[[+ Size \* .5]], onComplete = Destroy\_BadGuy} ) return BadGuy end

ahh, worked like a charm, thanks a lot panc!

#1: You have the BG variable set to two different objects. 

#2: You need to have your function that you are calling, declared ahead of where you are calling it.

#3: You’re better off using a (obj) call on your removal function when your transition is completed.

I updated some of your code. Try it out now:

local BG1 = {} local BadGuyNumb = 0 ---------------------------------------------------------------------------------------- -- Display, add Physics and insert BadGuys into a table ---------------------------------------------------------------------------------------- local function Destroy\_BadGuy (obj) print("obj remove") display.remove( obj ) obj = nil end local function SpawnBadGuys () local Size = math.random(20,90) local BadGuy = display.newRect(0,0,40,40 ) table.insert( BG1, BadGuy) physics.addBody( BadGuy) BadGuy.isSensor = true transition.to( BadGuy,{time = math.random(1000,3000), y = screenBottom --[[+ Size \* .5]], onComplete = Destroy\_BadGuy} ) return BadGuy end

ahh, worked like a charm, thanks a lot panc!