Lua Help

So I’m trying to create an app dealing mostly with single taps on the display and need some help with counting the number of taps and then displaying that count after every tap occurs. I know that you can use the event listener to count taps, but how exactly do I go about displaying that count?

Basically I’m placing a small jpeg image on the display everywhere that the screen is touched and then keeping a count of how many touches/images have been put onto the screen. I’ve gotten the image to show and stay on the screen, but I can’t figure out how to keep a count and have it display correctly.

Thanks for the help! [import]uid: 174178 topic_id: 33888 reply_id: 333888[/import]

To display text on the screen you use display.newText. The documentation has some examples here: http://docs.coronalabs.com/api/library/display/newText.html

As far as keeping track of the taps you would just need to have a variable that you increment on each tap and then update your text object with the count. Quickie example below:
[lua]–here to the end of the function is what answers your question
local iTapCounter = 0

local myText = display.newText(iTapCounter, 0, 0, native.systemFont, 24)
myText:setTextColor(255, 255, 255)

local function objectTapped(event)
iTapCounter = iTapCounter + 1
myText.text = iTapCounter
end

–just creating a sample image
local mySmallImage = display.newImageRect(“myimage.jpg”, 150, 150)
mySmallImage.x = display.contentWidth /2
mySmallImage.y = display.contentHeight /2
mySmallImage:addEventListener(“tap”, objectTapped)[/lua] [import]uid: 147305 topic_id: 33888 reply_id: 134741[/import]

thanks a lot for your help.

one more question…

im trying to register taps on “objects” such as my background image and a solid rectangle in the center of the screen. but my evvent listener doesnt distinguish between the two objects. for example, when i tap the background… it registers as a tap on that object and adds a count to the background tap counter butttt when i tap the rectangle object that is displayed on top of the background image… it registers a tap on both images thus adding on to both counters.

basically i have a target (rectangle) and i want to add a counter on how many times a tap hits that target and how many times it doesnt hit that target (aka background tap) [import]uid: 174178 topic_id: 33888 reply_id: 134762[/import]

Try adding

return true  

at the end of your functions. What return true does is tell the simulator that you’re done with that tap and to stop everything after your “return true” statement. It sort of completes the event.

So what you want to do is at the end of your rectangle object listener function you add return true and your background listener should not go through anymore if you tap your rectangle.

Just a heads up for future reference, return true doesn’t work with tap and touch listeners. So if you have two images overlapping and ones a touch event and ones a tap, they will both go through even if you add return true. [import]uid: 77199 topic_id: 33888 reply_id: 134775[/import]

To display text on the screen you use display.newText. The documentation has some examples here: http://docs.coronalabs.com/api/library/display/newText.html

As far as keeping track of the taps you would just need to have a variable that you increment on each tap and then update your text object with the count. Quickie example below:
[lua]–here to the end of the function is what answers your question
local iTapCounter = 0

local myText = display.newText(iTapCounter, 0, 0, native.systemFont, 24)
myText:setTextColor(255, 255, 255)

local function objectTapped(event)
iTapCounter = iTapCounter + 1
myText.text = iTapCounter
end

–just creating a sample image
local mySmallImage = display.newImageRect(“myimage.jpg”, 150, 150)
mySmallImage.x = display.contentWidth /2
mySmallImage.y = display.contentHeight /2
mySmallImage:addEventListener(“tap”, objectTapped)[/lua] [import]uid: 147305 topic_id: 33888 reply_id: 134741[/import]

thanks a lot for your help.

one more question…

im trying to register taps on “objects” such as my background image and a solid rectangle in the center of the screen. but my evvent listener doesnt distinguish between the two objects. for example, when i tap the background… it registers as a tap on that object and adds a count to the background tap counter butttt when i tap the rectangle object that is displayed on top of the background image… it registers a tap on both images thus adding on to both counters.

basically i have a target (rectangle) and i want to add a counter on how many times a tap hits that target and how many times it doesnt hit that target (aka background tap) [import]uid: 174178 topic_id: 33888 reply_id: 134762[/import]

Try adding

return true  

at the end of your functions. What return true does is tell the simulator that you’re done with that tap and to stop everything after your “return true” statement. It sort of completes the event.

So what you want to do is at the end of your rectangle object listener function you add return true and your background listener should not go through anymore if you tap your rectangle.

Just a heads up for future reference, return true doesn’t work with tap and touch listeners. So if you have two images overlapping and ones a touch event and ones a tap, they will both go through even if you add return true. [import]uid: 77199 topic_id: 33888 reply_id: 134775[/import]