Displaying Text ( How to display a variable inside text?)

Hey Guys,
So I bought a book on Corona & one of the projects I am supposed to create is a calculator. I’m trying to get the text to display a local display variable. Can someone show me how I would do this?

I would think it would go like this:

local display
local textDisplay = display.newText(display, 10, 10, “GrilledCheese BTN Toasted”, 20)

Also, sometimes in a calculator it does not display anything. Would I just say in that case that display = nil?

Thank you guys in advance!
Looking forward to learning more about Corona!

Matt :smiley: [import]uid: 50842 topic_id: 14408 reply_id: 314408[/import]

matthew-

So, you have a few funny things going on here.

  1. You don’t need to declare ‘display’. Display is defined by Corona. When you declare the variable name ‘display’ as a local variable…

[lua]local display – get rid of this[/lua]

You actually end up destroying the ‘display’ class that is defined by Corona. So you won’t create any text at all. So drop that line.

  1. You don’t need to put “display” in the function parameters. And the order that you put your function parameters is important. So the first line should be:

[lua]local textObj= display.newText(“GrilledCheese BTN Toasted”, 0,0, nil, 14);[/lua]

  1. Once your text has been created, use the ‘text’ property of you new text object to make changes:

[lua]textObj.text = “GrilledCheese with Bacon. Even Better.”[/lua]

I hope that helps. BTW, what is the name of the Corona book you got?

[import]uid: 10818 topic_id: 14408 reply_id: 53277[/import]

@duneunit
THANK YOU :smiley: Helps a bunch :wink:

This is the book I purchased - http://www.burtonsmediagroup.com/books/mobile-app-development-with-corona-getting-started/

It’s been amazing so far!

Matt :smiley: [import]uid: 50842 topic_id: 14408 reply_id: 53279[/import]

I seem to be doing something wrong. If you look in the screenshot below, you’ll notice that the iPhone is actually displaying the font name, GrilledCheese Toasted BTN; instead of nothing (nil)
Any suggestions?

Thanks!

Image and video hosting by TinyPic
[import]uid: 50842 topic_id: 14408 reply_id: 53281[/import]

txtDisplay=""
txt=display.newText(txtDisplay,10,10,“GrilledCheese Toasted BTN”,20) [import]uid: 7911 topic_id: 14408 reply_id: 53283[/import]

in line 11, use
local Display = display.newText("",0,0,nil,14)

and if GrilledCheese … is your font name, then make line 11 as so

local Display = display.newText("",0,0,“GrilledCheese BTN Toasted”,14)
another thing that was rightly asked by duneunit, why do you want to call the variable Display?

why do you not simply call it textDisplay or displayText ?

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14408 reply_id: 53284[/import]

Works perfect guys! Appreciate your help :wink:

@jayantv

I see instead of using nil you used “(nothing here)”

so basically when I want it to say nil, I can just say calcDisplay.text (yes i changed the name :wink: = “”

Thanks Again! [import]uid: 50842 topic_id: 14408 reply_id: 53285[/import]

Great that you got it working!

I made a mistake and didn’t notice that “GrilledCheese…” was the font name! But looks like you got it all sorted out! Good job! [import]uid: 10818 topic_id: 14408 reply_id: 53292[/import]

instead of using images for each button why not use display.newRect and use a custom font with display.newText
would this not be less memory usage [import]uid: 7911 topic_id: 14408 reply_id: 53304[/import]

I agree with jstrahan.


instead of using images for each button why not use display.newRect and use a custom font with display.newText
would this not be less memory usage

And it seems as though you already have a custom font (something that is making me very hungry right about now)!

Regards,

Andreas Ricci
NuPlay Entertainment
Founder & Lead Developer [import]uid: 7366 topic_id: 14408 reply_id: 53325[/import]

To the original question how to display a variable inside text :

[code]
local myVar = 22

local showText = display.newText("My Var = " … myVar, 100, 100, native.systemFont, 18)

–Or

local showText = display.newText("My Var = " … tostring(myVar), 100, 100, native.systemFont, 18)
[/code] [import]uid: 84637 topic_id: 14408 reply_id: 53366[/import]