:removeSelf not working

Hey guys I need some quick help with my code.

I have a functions display new text (previously called).

However when I use a timer and make it remove the text I does not work.

Here is the code

local currentWorkout function timeWorkout(event) currentWorkout:removeSelf() end function workoutStarted(event) currentWorkout = display.newText(activityCurrent, 100,500, native.systemFont, 46) currentWorkout:setTextColor(0,0,0) timer.performWithDelay( timeTotal, timeWorkout, 10 ) end 

Here is the error I am getting.

Attempt to call method ‘removeSelf’ (a nil value)

Thanks for the help :slight_smile:

When predeclaring currentWorkout, it is a placeholder with the value nil. currentWorkout is being removed while the value is still nil. There is an error because you’re telling the program to remove nil instead of the display object.

Here’s an example of what I mean.

[lua]local hi

–hi = display.newText(“Hello World”, 100,100,native.systemFont, 20)

–Program will throw an error until hi has a value. Uncomment the above code and it will work

hi:removeSelf()[/lua]

Edit - Oh I see you understand the concept. I skimmed the top part lol.

  • timer.performWithDelay( timeTotal, timeWorkout, 10 )
  • In this line, you are calling the removeSelf 10 times. The first one works and the second iteration causes failure

There we go!

Thanks for the advice. I tried it with a 1 earlier but it didn’t work for some reason xD

Thanks for the help!

When predeclaring currentWorkout, it is a placeholder with the value nil. currentWorkout is being removed while the value is still nil. There is an error because you’re telling the program to remove nil instead of the display object.

Here’s an example of what I mean.

[lua]local hi

–hi = display.newText(“Hello World”, 100,100,native.systemFont, 20)

–Program will throw an error until hi has a value. Uncomment the above code and it will work

hi:removeSelf()[/lua]

Edit - Oh I see you understand the concept. I skimmed the top part lol.

  • timer.performWithDelay( timeTotal, timeWorkout, 10 )
  • In this line, you are calling the removeSelf 10 times. The first one works and the second iteration causes failure

There we go!

Thanks for the advice. I tried it with a 1 earlier but it didn’t work for some reason xD

Thanks for the help!