1

I'm trying to use a for loop to destroy objects inside my array like so:

for item in self.objects do
    item:removeSelf()
end

The self.objects is my array and it contains images I use for animation. If I touch one of these animated objects, it should be destroyed (disappear). My problem is, I'm getting this error message:

Attempt to call a table value

I'm not sure why I'm getting this error and how to fix it so can somebody please explain how I can remove objects from my array during touch event and why I'm getting this message? Thanks in advance. :D

1 Answer 1

5

A generic for loop of the form for x in y do... expects y to be an iterator function. You're passing it a table, hence the error message.

If you just want to iterate over each entry in the table, use pairs:

for key, item in pairs(self.objects) do
  item:removeSelf()
end

See PiL 4.3.5 and all of Chapter 7 for more info on generic for and iterators.

Sign up to request clarification or add additional context in comments.

1 Comment

Since this is an array, you should use ipairs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.