tracking button taps

Hi,

I’m new to Corona and programming in general, so I decided to buy “Learning Mobile App and Game Development with Corona” which was written by Dr. Burton. 

The way he structures the chapters in this book is by walking the reader through an example, and then giving assignments at the end of the chapter that you have to apply the knowledge you’ve gained through the example.

Unfortunately, it’s only chapter 2 and I’m already stuck.

I’ve been asked to create an application that keeps track of how many times a button is tapped and display a running total on the screen. 

If someone could suggest a way to do this, I would appreciate it.

Thanks,

CC.

Can I recommend some tutorial videos to help you out generally as a Corona beginner?

http://www.youtube.com/user/cheetomoskeeto/videos?view=0

I used these when I first started using Corona, and they’re great. I would advise working through these before going through a book, they’re well explained which makes it easier to understand how Corona / Lua works. Some of it may be slightly outdated now, but I don’t think there’s is anything in there which won’t work, it’s more likely that things have been simplified further since those videos were made.

Onto your problem.

So firstly I set up any variables I might need:

--I always cache these values at the start of my program --since they are used so often to position objects local \_W = display.contentWidth local \_H = display.contentHeight --this is where we will store the number of taps local numTaps = 0 --make a rect which is full screen, this will be the object we tap

local touchableRect = display.newRect(0, 0, \_W, \_H) --let's make it red so that you can see the white text above it touchableRect:setFillColor(255, 0, 0)--this is the text object which will show the number of taps local numTapText = display.newText("0", \_W \* 0.5, \_H \* 0.5, native.systemFont, 30)  

Now you need a tap function:

local function screenTap(event) numTaps = numTaps + 1 numTapText.text = numTaps end

And now add this function to an object:

touchableRect:addEventListener("tap", screenTap)

And that should be it!

Now when you tap, the total number of taps should increase and the text on screen should be updated (by changing it’s .text property). 

Thanks so much for answering my question.

local numTaps = 0

numTaps = numTaps + 1

numTapText.text = numTaps

These were especially helpful as they weren’t covered in the textbook.

I’m now supplementing the textbook with another non-language specific introductory  programming book which is helping me understand the basics more. (I got stuck one too many times and it was recommended to me as a complete beginner).

I will definitely check out those video tutorials as well. 

Thanks again,

CC.

Can I recommend some tutorial videos to help you out generally as a Corona beginner?

http://www.youtube.com/user/cheetomoskeeto/videos?view=0

I used these when I first started using Corona, and they’re great. I would advise working through these before going through a book, they’re well explained which makes it easier to understand how Corona / Lua works. Some of it may be slightly outdated now, but I don’t think there’s is anything in there which won’t work, it’s more likely that things have been simplified further since those videos were made.

Onto your problem.

So firstly I set up any variables I might need:

--I always cache these values at the start of my program --since they are used so often to position objects local \_W = display.contentWidth local \_H = display.contentHeight --this is where we will store the number of taps local numTaps = 0 --make a rect which is full screen, this will be the object we tap

local touchableRect = display.newRect(0, 0, \_W, \_H) --let's make it red so that you can see the white text above it touchableRect:setFillColor(255, 0, 0)--this is the text object which will show the number of taps local numTapText = display.newText("0", \_W \* 0.5, \_H \* 0.5, native.systemFont, 30)  

Now you need a tap function:

local function screenTap(event) numTaps = numTaps + 1 numTapText.text = numTaps end

And now add this function to an object:

touchableRect:addEventListener("tap", screenTap)

And that should be it!

Now when you tap, the total number of taps should increase and the text on screen should be updated (by changing it’s .text property). 

Thanks so much for answering my question.

local numTaps = 0

numTaps = numTaps + 1

numTapText.text = numTaps

These were especially helpful as they weren’t covered in the textbook.

I’m now supplementing the textbook with another non-language specific introductory  programming book which is helping me understand the basics more. (I got stuck one too many times and it was recommended to me as a complete beginner).

I will definitely check out those video tutorials as well. 

Thanks again,

CC.