Randomize texts

Hey huys,
So I have these texts:

text 1 = display.newText("Text 1", 20, 40, native.systemFont, 20)  
text 2 = display.newText("Text 2", 20, 40, native.systemFont, 20)  
text 3 = display.newText("Text 3", 20, 40, native.systemFont, 20)  
text 4 = display.newText("Text 4", 20, 40, native.systemFont, 20)  
text 5 = display.newText("Text 5", 20, 40, native.systemFont, 20)  

I need one of them to be randomly selected to appear each time the scene starts. How do I do that? Can someone help me with the code?

Thanks!
[import]uid: 95495 topic_id: 20369 reply_id: 320369[/import]

First, put all of your objects into a table like so:

[lua]local table = {
“Text1”,
“Text2”,
“Text3”,
“Text4”,
}[/lua]

Then when you want the scene to start, create new text with the value from one of the random table items inside of it:

[lua]display.newText(table[math.random(1,#table)], 0, 0, native.systemFont, 50)[/lua]

Hope this helps!

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 20369 reply_id: 79600[/import]

Beautifully elegant solution, Jordan! [import]uid: 52491 topic_id: 20369 reply_id: 79658[/import]

It didn’t quite work, but I managed something with a table, thanks!

Another question:
My texts are long. How do I break them into more lines? And then make them centralized?

Thanks! [import]uid: 95495 topic_id: 20369 reply_id: 80065[/import]

display.newText() has two more parameters that specify the width and optional height of the box to fit the text into and it will automatically wrap if you use this:

local text1 = display.newText(string, x, y, width, height, font, fontSize)
That should help you out. [import]uid: 19626 topic_id: 20369 reply_id: 80085[/import]

Hm this isn’t working! It worked with width, but when I set height to 0 as it tells me to, nothing happens, and if I set like 500, it just distorts the whole text instead of adding lines.
Is there another way? [import]uid: 95495 topic_id: 20369 reply_id: 80482[/import]