Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

self.tapped command

edited July 2012 in Suggestions Posts: 15

In Codea 1.4.4 there should be a self.tapped command to easily make buttons. Like this:

function Button:touched(touch)
    if self.tapped then
           print("Tapped Button")
     else
           print("Tapped Elsewhere")
end
end
Tagged:

Comments

  • edited July 2012 Posts: 15

    But there's probably some syntax that does that :P I'm new to lua.

  • edited July 2012 Posts: 489

    It is not clear to me what something built-in could usefully add compared to a button-specific Button:isHit(touch) function added to your Button class. If you search this forum, or other Codea-related materials, you will find a lot of advice on buttons. The pattern below may be of some help:

    function Button:init(name, position, onTouch, parent)
        self.name = name
        self.position = position
        self.onTouch = onTouch -- a function called when button touched
        self.parent = parent -- in some cases, you may want to keep track of the parent
        ...
    end
    
    function Button:isHit(touch)
        if [hit logic, based on self.position] then
            return true
        end
        return false
    end
    
    function Button:touched(touch)
        if touch.state == BEGAN and self:isHit(touch) then
            self:onTouch()
            return true -- Returns true if touch was handled
        end
        return false
    end
    
    function setup()
        ...
        onTouch = function (self)
            print("Button named "..self.name.." was touched!")
        end
        myButton1 = Button("Button 1", position, onTouch, nil)
        ...
    end
    
    function touched(touch)
        ...
        if myButton1:touched(touch) then -- Pass touch to myButton
            -- Do something if touch was handled by button
        else
            -- Do something else if touch was not handled by button
        end
        ...
    end
    
    
  • Posts: 15

    I'm eleven years old anyway, so yeah......

  • Posts: 15

    I would lock this considering I made a button tutorial that uses only a couple lines of code, but I'm not a mod......

Sign In or Register to comment.