event listener for touching text

Hi

I am trying to build a very simple app emulating flash cards for memorizing. 

I would like to have the app listen for and react to a touch to a particular bit of text, i.e., the choices in a multiple choice question.

I display the answer choices on screen like this

local textObject = display.newText("a. " … questions[i].right, 120, 200, native.systemFontBold, 15 )

local textObject = display.newText("b. " … questions[i].wrong1, 120, 240, native.systemFontBold, 15 )

local textObject = display.newText("c. " … questions[i].wrong2, 120, 280, native.systemFontBold, 15 )

I added a function and event listener thus:

function touchScreen(event)

    if event.phase == “began” then

        

        local textObject = display.newText(“you touched the screen!”, 220, 380, native.systemFontBold, 15 )

    

    end

end

Runtime:addEventListener(“touch”, touchScreen)

but this only ‘hears’ touches to the screen anywhere on the screen. I’m not sure how to to proceed from here. Have read several tutorials but am stuck. Any help very appreciated!

Hi @d2gp,

Did you read this guide on basic interactivity?

http://docs.coronalabs.com/guide/events/detectEvents/index.html

Basically, follow the “Understanding Hit Events” part of that guide, and you should soon be on your way.

Brent

thanks, I will check that out.

hi again

ok so thanks for the link - thanks to that I got a function and some event listeners working on my textObjects e.g.

local function myTouchListener( event )

if ( event.phase == “began” ) then    

        local textObject = display.newText(“you touched the text!”, 320, 480, native.systemFontBold, 15 ) 

    end

    return true

end

local textObject2 = display.newText("a. " … questions[i].right, 120, 200, native.systemFontBold, 15 )

textObject2:addEventListener( “touch”, myTouchListener )

I have a few different text objects and they are all using the same function, myTouchListener. This function needs to know which textObject has been touched. If textObject1 is touched, then one message is displayed. If textObject2 is touched, a different one. I’m not sure how to proceed from here. Thanks for the help.

Hi @d2gp,

If you look at the next code sample on that guide (following the one you pulled this from), notice that the touched object is revealed by “event.target” in the “myTouchListener” function. For example:

[lua]

local function myTouchListener( event )

  if ( event.phase == “began” ) then

    print( “object touched =”, event.target )  

    local textObject = display.newText("you touched "…event.target , 320, 480, native.systemFontBold, 15 ) 

  end

  return true

end

[/lua]

So, you can do all sorts of things with that knowledge… assign properties to the original (touchable) object and then read those in the touch function, move the object, transition it upon touch, remove it entirely… basically whatever you want to do, since you know the internal Lua ID associated with it.

Brent

Thanks for your help Brent.

When I use  local textObject = display.newText("you touched "…event.target , 320, 480, native.systemFontBold, 15 ) 

I get a simulator runtime error: Attempt to concatenate field ‘target’ (a table value)


The problem is I have a question and 3 answers on screen. One right answer and two wrong ones. The questions and answers are stored in a table. I somehow need to figure out, inside the myTouchListener function, which answer the user touched. Does event.target hold any information about itself other than its memory address? Can I use it to distinguish between the 3 answers? 

Hi @d2gp,

Yes, you can add any property to the object (the text) and read it during the touch listener.

For example, you could add any number of properties as necessary:

[lua]

local textObject2 = display.newText("a. " … questions[i].right, 120, 200, native.systemFontBold, 15 )

textObject2.answerNumber = 2

textObject2.isCorrect = true

–more if you need

[/lua]

Then, in the listener, you just read those like:

[lua]

print( "you touched answer number "…event.target.answerNumber )

if ( event.target.isCorrect == true ) then  

  print( “this is the correct answer!!!” )

end

[/lua]

Hi

OK that looks like what I need. Thanks so much.

I have ordered a copy of the classic ‘Programming in Lua’, yet I suspect that there is a lot more to programming in Corona than just Lua. Is there a good book on Corona programming that you could recommend?

cheers,

David

Hi David,

Certainly, I recommend any of the books on our “Books and Courses” page. You can decide which one suits your needs.

http://www.coronalabs.com/resources/books/

Take care,

Brent

Hi 

I’m stuck again. I have questions and answers for let’s say 50 flashcards, all stored in a table. Clearly I need to use some sort of loop to control the iteration through this table. Perhaps a while or a for loop.

What I don’t know how to do is pause the loop and wait for the user to answer each card in turn. Once the user has answered, then the loop can recommence and display the next card and the process repeats. But how?!? 

thanks in advance,

David

Never mind, I figured it out.  :slight_smile:

Hi @d2gp,

Did you read this guide on basic interactivity?

http://docs.coronalabs.com/guide/events/detectEvents/index.html

Basically, follow the “Understanding Hit Events” part of that guide, and you should soon be on your way.

Brent

thanks, I will check that out.

hi again

ok so thanks for the link - thanks to that I got a function and some event listeners working on my textObjects e.g.

local function myTouchListener( event )

if ( event.phase == “began” ) then    

        local textObject = display.newText(“you touched the text!”, 320, 480, native.systemFontBold, 15 ) 

    end

    return true

end

local textObject2 = display.newText("a. " … questions[i].right, 120, 200, native.systemFontBold, 15 )

textObject2:addEventListener( “touch”, myTouchListener )

I have a few different text objects and they are all using the same function, myTouchListener. This function needs to know which textObject has been touched. If textObject1 is touched, then one message is displayed. If textObject2 is touched, a different one. I’m not sure how to proceed from here. Thanks for the help.

Hi @d2gp,

If you look at the next code sample on that guide (following the one you pulled this from), notice that the touched object is revealed by “event.target” in the “myTouchListener” function. For example:

[lua]

local function myTouchListener( event )

  if ( event.phase == “began” ) then

    print( “object touched =”, event.target )  

    local textObject = display.newText("you touched "…event.target , 320, 480, native.systemFontBold, 15 ) 

  end

  return true

end

[/lua]

So, you can do all sorts of things with that knowledge… assign properties to the original (touchable) object and then read those in the touch function, move the object, transition it upon touch, remove it entirely… basically whatever you want to do, since you know the internal Lua ID associated with it.

Brent

Thanks for your help Brent.

When I use  local textObject = display.newText("you touched "…event.target , 320, 480, native.systemFontBold, 15 ) 

I get a simulator runtime error: Attempt to concatenate field ‘target’ (a table value)


The problem is I have a question and 3 answers on screen. One right answer and two wrong ones. The questions and answers are stored in a table. I somehow need to figure out, inside the myTouchListener function, which answer the user touched. Does event.target hold any information about itself other than its memory address? Can I use it to distinguish between the 3 answers? 

Hi @d2gp,

Yes, you can add any property to the object (the text) and read it during the touch listener.

For example, you could add any number of properties as necessary:

[lua]

local textObject2 = display.newText("a. " … questions[i].right, 120, 200, native.systemFontBold, 15 )

textObject2.answerNumber = 2

textObject2.isCorrect = true

–more if you need

[/lua]

Then, in the listener, you just read those like:

[lua]

print( "you touched answer number "…event.target.answerNumber )

if ( event.target.isCorrect == true ) then  

  print( “this is the correct answer!!!” )

end

[/lua]

Hi

OK that looks like what I need. Thanks so much.

I have ordered a copy of the classic ‘Programming in Lua’, yet I suspect that there is a lot more to programming in Corona than just Lua. Is there a good book on Corona programming that you could recommend?

cheers,

David

Hi David,

Certainly, I recommend any of the books on our “Books and Courses” page. You can decide which one suits your needs.

http://www.coronalabs.com/resources/books/

Take care,

Brent

Hi 

I’m stuck again. I have questions and answers for let’s say 50 flashcards, all stored in a table. Clearly I need to use some sort of loop to control the iteration through this table. Perhaps a while or a for loop.

What I don’t know how to do is pause the loop and wait for the user to answer each card in turn. Once the user has answered, then the loop can recommence and display the next card and the process repeats. But how?!? 

thanks in advance,

David