Help on object position

the terminal doesn’t print my.name even though the object is passing my parameters. Here is the code.

oh and H1 is display.contentHeight

if circle.y \> H1 \* 0.2 then print(my.name) end

If you post more code I think you could get some help. 

ok so I fixed it by putting it into a function like so

 my = { name = "Max", age = "16", } local function remover() if myCircle.y \> H1\*0.5 then myCircle.alpha=0.5 display.remove(self) print(my.name) return true end end Runtime:addEventListener("enterFrame",remover)

It prints my.name which is Max into the terminal but display.remove(self) doesn’t do anything. Not even errors. I need the object to remove itself each time it passes halfway through the screen.

You are trying to remove ‘self’ but the remover function doesn’t know what ‘self’ is as it hasn’t been passed anything.

If it is just myCircle you want to remove you can do display.remove(myCircle).

If you are creating multiple ‘myCircles’ and more than one of them can exist at any one time, you will need to put them in a table and

loop through to find each of their ‘Y’ positions and remove as required.

[lua]

local myCircles = {}

local function spawner()

      local myCircle =

      --put your spawn code here

      myCircles[#myCircles+ 1] = myCircle

            – add freshly spawned object to myCircles table

end

local function remover()

      for i = #myCircles, 1 , - 1 do

                 

            local obj = myCircles[i]

            if obj ~= nil then

                  if obj.y > H1\ *0.5 then

                        display.remove(obj)

                        myCircles[i] = nil

                  end

            end

      end

end

Runtime:addEventListener(“enterFrame”,remover)

[/lua]

Nice! I modified it to fit my needs and it works great thanks a ton Nick! Here is the complete code for anyone else!

local myCircles = {} local function spawner() local tmp = display.newImageRect(catGroup,"cat.png",50,50) tmp.x = math.random(0,W1) tmp.y = H1\*-0.2 physics.addBody(tmp,"dynamic",{bounce=1,friction=1,radius=25}) myCircles[#myCircles+1] = tmp -- add freshly spawned object to myCircles table end timer.performWithDelay(1000,spawner,0) local function remover() for i = #myCircles, 1, -1 do local tmp = myCircles[i] if tmp ~= nil then if tmp.y \> H1\*0.5 then print(my.name) display.remove(tmp) myCircles[i] = nil end end end end Runtime:addEventListener("enterFrame",remover)

Do you have an idea on how you would do this with a static object moving along with this code?

 local function backgroundScroll(self,event) self.y = self.y - self.speed end Runtime:addEventListener("enterFrame", myCircle) myCircle.enterFrame=backgroundScroll

I run the code, the cat is spawn well, the problem is removing.

Sometimes,the cat is removed in the center and sometimes, the cat is not removed

I dont know how to fix that.

[lua]–

local myCircles = {}

 local H1=display.contentHeight

local function spawner()

     local tmp = display.newImageRect(“cat.png”,50,50)

tmp.x = math.random(0,display.contentWidth)

tmp.y = display.contentHeight*-0.2

physics.addBody(tmp,“dynamic”,{bounce=1,friction=1,radius=25})

      myCircles[#myCircles+1] = tmp

            – add freshly spawned object to myCircles table

end

timer.performWithDelay(1000,spawner,0)

local function remover()

      for i = #myCircles, 1, -1 do

                 

            local tmp = myCircles[i]

            if tmp ~= nil then

                  if tmp.y > H1*0.5 then

                  --print(my.name)

                        display.remove(tmp)

                        myCircles[i] = nil

                  end

            end

      end

end

Runtime:addEventListener(“enterFrame”,remover)

[/lua]

Try adding table.remove( myCircles,i ) after display.remove(tmp).

I added table.remove( myCircles,i ) after display.remove(tmp),

but everything is the same, 

the Runtime doesn’t remove all the cats in the middle of the screen.

Let the cats spawn with more time in-between each one and it works fine for me.

I set 1500 for spawn, it works. but I still see a cat or two if the spawn is there for a long time.

But If I put 500 spawn , it doesn’t remove correctly.

I don’t understand Why?

If you post more code I think you could get some help. 

ok so I fixed it by putting it into a function like so

 my = { name = "Max", age = "16", } local function remover() if myCircle.y \> H1\*0.5 then myCircle.alpha=0.5 display.remove(self) print(my.name) return true end end Runtime:addEventListener("enterFrame",remover)

It prints my.name which is Max into the terminal but display.remove(self) doesn’t do anything. Not even errors. I need the object to remove itself each time it passes halfway through the screen.

You are trying to remove ‘self’ but the remover function doesn’t know what ‘self’ is as it hasn’t been passed anything.

If it is just myCircle you want to remove you can do display.remove(myCircle).

If you are creating multiple ‘myCircles’ and more than one of them can exist at any one time, you will need to put them in a table and

loop through to find each of their ‘Y’ positions and remove as required.

[lua]

local myCircles = {}

local function spawner()

      local myCircle =

      --put your spawn code here

      myCircles[#myCircles+ 1] = myCircle

            – add freshly spawned object to myCircles table

end

local function remover()

      for i = #myCircles, 1 , - 1 do

                 

            local obj = myCircles[i]

            if obj ~= nil then

                  if obj.y > H1\ *0.5 then

                        display.remove(obj)

                        myCircles[i] = nil

                  end

            end

      end

end

Runtime:addEventListener(“enterFrame”,remover)

[/lua]

Nice! I modified it to fit my needs and it works great thanks a ton Nick! Here is the complete code for anyone else!

local myCircles = {} local function spawner() local tmp = display.newImageRect(catGroup,"cat.png",50,50) tmp.x = math.random(0,W1) tmp.y = H1\*-0.2 physics.addBody(tmp,"dynamic",{bounce=1,friction=1,radius=25}) myCircles[#myCircles+1] = tmp -- add freshly spawned object to myCircles table end timer.performWithDelay(1000,spawner,0) local function remover() for i = #myCircles, 1, -1 do local tmp = myCircles[i] if tmp ~= nil then if tmp.y \> H1\*0.5 then print(my.name) display.remove(tmp) myCircles[i] = nil end end end end Runtime:addEventListener("enterFrame",remover)

Do you have an idea on how you would do this with a static object moving along with this code?

 local function backgroundScroll(self,event) self.y = self.y - self.speed end Runtime:addEventListener("enterFrame", myCircle) myCircle.enterFrame=backgroundScroll

I run the code, the cat is spawn well, the problem is removing.

Sometimes,the cat is removed in the center and sometimes, the cat is not removed

I dont know how to fix that.

[lua]–

local myCircles = {}

 local H1=display.contentHeight

local function spawner()

     local tmp = display.newImageRect(“cat.png”,50,50)

tmp.x = math.random(0,display.contentWidth)

tmp.y = display.contentHeight*-0.2

physics.addBody(tmp,“dynamic”,{bounce=1,friction=1,radius=25})

      myCircles[#myCircles+1] = tmp

            – add freshly spawned object to myCircles table

end

timer.performWithDelay(1000,spawner,0)

local function remover()

      for i = #myCircles, 1, -1 do

                 

            local tmp = myCircles[i]

            if tmp ~= nil then

                  if tmp.y > H1*0.5 then

                  --print(my.name)

                        display.remove(tmp)

                        myCircles[i] = nil

                  end

            end

      end

end

Runtime:addEventListener(“enterFrame”,remover)

[/lua]

Try adding table.remove( myCircles,i ) after display.remove(tmp).

I added table.remove( myCircles,i ) after display.remove(tmp),

but everything is the same, 

the Runtime doesn’t remove all the cats in the middle of the screen.

Let the cats spawn with more time in-between each one and it works fine for me.