kevin ver
kevin ver

Reputation: 851

Remove/Delete only one object out a table

I am using the underneath code to delete the object I am colliding with, But its deleting all the object in the table, how can I limed this to the object in the table I'm colliding with? (so it only deletes one)

for i = #badC1T, 1, -1 do
    if badC1T[i] ~= nil then

      local function dellBadC1T()
        if badC1T[i] ~= nil then
          badC1T[i]:removeSelf()
          badC1T[i] = nil
        end
      end

    transition.to( badC1T[i], {  time=100, alpha=0, onComplete = dellBadC1T} )
    end
end

Upvotes: 0

Views: 78

Answers (1)

catwell
catwell

Reputation: 7020

I don't know how your code works, but given that the loop is in reverse, is the object you are colloding with the last one in the table that is not nil?

I suspect you just want to exit the loop after removing one object, in which case you just have to break the loop:

for i = #badC1T, 1, -1 do
  if badC1T[i] ~= nil then

    local function dellBadC1T()
      if badC1T[i] ~= nil then
        badC1T[i]:removeSelf()
        badC1T[i] = nil
      end
    end

    transition.to( badC1T[i], {  time=100, alpha=0, onComplete = dellBadC1T} )
    break -- <= just add this
  end
end

Upvotes: 1

Related Questions