I am trying to create a text display, but for some reason the width of the object is nil, despite the fact that it has text. Here is my code:
File1.lua:
local myTextDisplay = display.newText("2", display.contentWidth / 2, display.contentHeight / 2, native.systemFontBold)
File2.lua:
local myTextDisplay = File1:getTextDisplay() --function in File1.lua that returns myTextDisplay
print("Displayed text: " .. myTextDisplay.text)
if myTextDisplay.isVisible then print("Text display is visible") end
print("Text display width: " .. myTextDisplay.width)
The first time I open the File2 scene, this works fine, and prints:
Displayed text: 2
Text display is visible
Text display width: 10.6599...
However, when I open the same scene a second time (it is not recycled), the last print statement causes a runtime error:
Displayed text: 2
Text display is visible
Runtime error
C:\....myGame.lua:20: attempt to concatenate field 'width' (a nil value)
How can this be? If the display object contains text, how can it possibly have a nil width? Does it have something to do with the fact that I am getting the same display object each time I open the non-recycled File2 scene? Any advice appreciated!