0

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!

Sarah
  • 300
  • 2
  • 8
  • The text of an object is just a property. Same with width. You can indeed have a text while having a nil width. The question is why is the width nil. I think you should provide more code. Especially the sections where you get the stuff from file1 to file2. – Piglet Jun 23 '16 at 20:05

1 Answers1

0

I haven't been able to fix the original issue, but I think that there may be some problems inherent in passing display objects between scenes, especially when one scene is recycled.

Instead, I found the best workaround to be simply having two display objects, one in each scene. Then I pass the text and other properties of the object from the first scene to the second scene, and create the second display object to be identical to the first.

Passing data between scenes seems to work much more smoothly than passing display objects.

Sarah
  • 300
  • 2
  • 8