1
votes

I've unlimited arrow object ...

local function deleteit(obj)
  display.remove(obj)
end

local function createArrow()
  local arrow = display.newImageRect("images/right",64,64)
  arrow.x = centerX
  arrow.y = centerY
  transition.to(arrow,{time = 1000, x = 0 , y = 0 , onComplete = deleteit(arrow)})
end

timer.performWithDelay(1000,createArrow,0)

But when I run this game, all of my arrow vanish. I know why they vanished but I don't know how to fix this code. Please help me.

PS. I cannot use array because of memory problems.

1

1 Answers

2
votes

The problem is that when you assign the callback for onComplete you are actually calling the deleteit function and therefore you are deleting the object before the timer expires.

The callback wants a reference to a function, but you are actually calling the function instead of just getting a reference.

Try this:

local function createArrow()
   local arrow = display.newImageRect("images/right",64,64)
   arrow.x = centerX
   arrow.y = centerY

   local cb = function()
     deleit( arrow )
   end

   transition.to(arrow, {time=1000, x=0, y=0, onComplete=cb} )
end