Search code examples
buttontextcoronasdk

How can I use a text as a button in Corona SDK?


Hello I am new in Corona SDK and I want to help me with my problem: Is it possible to use a text as button in Corona, and if yes can you show me how to do it? Thank you.


Solution

  • Each object in Corona can be assigned an event listener, which will respond to whatever event your subscribe it to. For a text to act as a button, you can do the following:

    1. Create text object

      local txtObj = display.newText('some text', 50, 50, native.systemFont, 20)
      
    2. Create a method that will be executed upon text touch, eg.:

      local function onClick(event)
          print('I've been clicked')
      end
      
    3. Add event listener to your text object

      txtObj:addEventListener('touch', onClick)
      

    Now touching your text object will call onClick method, hence you're text will act like a button.

    This is the simplest example, and there is much more to learn about events. See great documentation here and here.