Centre Text on a screen coordinate

HI,
Can anyone suggest the best way of display text so that it is centred on a certain point on the screen given by the coordinates x,y.

The text to be displayed will vary and be stored in a variable.

Thanks in advance for any help

Richard [import]uid: 7830 topic_id: 6833 reply_id: 306833[/import]

I use a global variable like this:

–Load Global variables
screenCenterX = display.viewableContentWidth / 2
screenCenterY = display.viewableContentHeight /2
Then I use it like this:

local myText = display.newText(“TextHere”, 0, 0, sysFont, 26)
myText.x = screenCenterX; myText.y = screenCenterY [import]uid: 11809 topic_id: 6833 reply_id: 23848[/import]

HI,

Surely this centres it on the screen, I need to centre the text based on a particular point eg 300,200.

Also the text being displayed is in a variable and can change when it is being updated, bu I need it to remain centred.

Thanks [import]uid: 7830 topic_id: 6833 reply_id: 23849[/import]

That will keep it centered but if you give it 300,200, when your app runs on different devices, how will that effect the display? I always us the above then use a factor of it.

Example:

myTxt.x = screenCenterX * .2 ; myTxt.y = screenCenterY * 1.5 [import]uid: 11809 topic_id: 6833 reply_id: 23851[/import]

Hi Herlyrg,

If you want it centered at 300, 200
the default is centered (mid point of text vertically and horozontally) the following url may help.
http://developer.anscamobile.com/reference/index/objectsetreferencepoint

– Center Justified text, for variable length text
– object:setReferencePoint( referencePoint )
– display.TopCenterReferencePoint
local mtitle1 = “This is the Main Title”
local title1 = display.newText(mtitle1, 290, 70, nill,18)
title1:setTextColor(0,255,0)
title1:setReferencePoint(display.CenterReferencePoint)
title1.x = 300; title1.y = 200 [import]uid: 8102 topic_id: 6833 reply_id: 64645[/import]

As a newbie, I always like to find out what corona can do. I did some code to print the text at centre. But I find out if I don’t clear the backgroud then all the text will print ontop of each other.
I build a btn to add the value again and again.

I add the clearspace function to cover the previous text, I want to know any other good method to do that.

Sorry to ask such question, but my last programming experience is many years ago with BASIC on my apple.

Hope someone can help.

KC
Bonvivid

module(…, package.seeall)
function new()

local localGroup = display.newGroup()

local _H = display.contentHeight
local _W = display.contentWidth

local movieclip = require(“movieclip”)

local function AreUready()
local total = 100
local park

local dataGroup = display.newGroup()

localGroup:insert(dataGroup)

local clearspace = function( event )

if event.phase == “release” then
local display_txt2 = display.newRect(0,0,100,100);
display_txt2:setFillColor(255,255,255)
display_txt2:setReferencePoint(display.CenterReferencePoint);
display_txt2.x = _W/2; display_txt2.y = _H/2;
dataGroup:insert(display_txt2)
addtotal()
end
end
addtotal = function( event )

total = total + 999
local display_txt = display.newText(total, 0, 0 , native.systemFont, 32);
display_txt:setTextColor(255,0,255)
display_txt:setReferencePoint(display.CenterReferencePoint);
display_txt.x = _W/2; display_txt.y = _H/2;
dataGroup:insert(display_txt)

end

totalbtn = ui.newButton{
defaultSrc = “playnow-btn.png”,
defaultX = 59,
defaultY = 59,
overSrc = “playnow-btn-over.png”,
overX = 59,
overY = 59,
onEvent = clearspace,
id = “total”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}

totalbtn.xOrigin = 180; totalbtn.yOrigin = 300
totalbtn.isVisible = true
localGroup:insert( totalbtn)

end

local function initVars ()

AreUready()

end

initVars()

return localGroup

end [import]uid: 94613 topic_id: 6833 reply_id: 65425[/import]

mycorona - The issue is in the addTotal function. In the function you are creating new text object everytime, which is being displayed on top of the previous text. The better option is to just set the text of the Text object by using display_txt.text = total and thus updating the same text object.

If you notice i am creating the text object only once and then setting it with total value on click of the button…

below is corrected code ( i made some modification to get this working on my system):
module(…, package.seeall)
function new()

local ui = require(“ui”)

local localGroup = display.newGroup()

local _H = display.contentHeight
local _W = display.contentWidth

–local movieclip = require(“movieclip”)
local display_txt
local dataGroup = display.newGroup()
display_txt = display.newText("", 0, 0 , native.systemFont, 32);
display_txt:setTextColor(255,0,255)
display_txt:setReferencePoint(display.CenterReferencePoint);
display_txt.x = _W/2; display_txt.y = _H/2;
dataGroup:insert(display_txt)
local function AreUready()
local total = 100
local park

localGroup:insert(dataGroup)

local clearspace = function( event )
if event.phase == “release” then
addtotal()
end
end

addtotal = function( event )

total = total + 999

display_txt.text = total

end
totalbtn = ui.newButton{
defaultSrc = “playnow-btn.png”,
defaultX = 59,
defaultY = 59,
overSrc = “playnow-btn.png”,
overX = 59,
overY = 59,
onEvent = clearspace,
id = “total”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}

totalbtn.xOrigin = 180; totalbtn.yOrigin = 300
totalbtn.isVisible = true
localGroup:insert( totalbtn)

end

local function initVars ()

AreUready()

end

initVars()

return localGroup

end

Don’t hesitate to ask questions…We are all learning corona…

cheers,
Bejoy

[import]uid: 84539 topic_id: 6833 reply_id: 65530[/import]

This line is magic.

“display_txt.text = total”

Thanks a lot.

KC
Bonvivid [import]uid: 94613 topic_id: 6833 reply_id: 65615[/import]

I just posted some code in a separate post on helper functions to align objects - that might be of help. No point cross posting it here but you can see it at:

http://developer.anscamobile.com/forum/2011/11/04/trying-implement-guis-faster [import]uid: 11393 topic_id: 6833 reply_id: 65631[/import]