How to display text on an image?

How do you display a text on top of an image ?

local ball = display.newImage( “ball.png”)

How would you put the letter “A” on the image of the ball? [import]uid: 10357 topic_id: 3398 reply_id: 303398[/import]

Here’s one way:

local ball = display.newImage( "ball.png")   
local myText = display.newText("B", 0, 0, native.systemFont, 24)   
myText:setTextColor(255, 255, 255)   
myText.x = ball.x  
myText.y = ball.y  

Tim [import]uid: 8196 topic_id: 3398 reply_id: 10155[/import]

Thanks for the prompt reply.

The above solution works great when the image is static (or not moving).
But if physics is into play and the ball is constantly colliding/bouncing, the text does not stay on the ball. [import]uid: 10357 topic_id: 3398 reply_id: 10157[/import]

Good question! One idea would be to use an enterFrame listener function that updates the text object’s x/y position to match the ball’s.

[code]
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 5)

local ball = display.newImage( “ball.png”)
physics.addBody(ball, {})

local myText = display.newText(“B”, 0, 0, native.systemFont, 40)
myText:setTextColor(255, 255, 255)
myText.x = ball.x
myText.y = ball.y

function updateLocation( event )
myText.x = ball.x
myText.y = ball.y
end

Runtime:addEventListener(“enterFrame”, updateLocation)
[/code] [import]uid: 8196 topic_id: 3398 reply_id: 10162[/import]