Howdy, Stranger!

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

Buttons

edited November 2014 in Questions Posts: 25

I am currently working on creating my own buttons.

Originally, my idea was to add 1 to some variable, each time my button is pressed. However, I have a problem. Every time I press the button it increases the variable a number of times as my "single" click is to considered by the iPad to be somewhat lengthy.

I tried to find the millis function to monitor the time between two consequent touches, but I couldn't.

What is the usual workaround this problem?

Thank you,
Alex

Comments

  • Jmv38Jmv38 Mod
    Posts: 3,295

    maybe: add 1 only when touch.state== BEGAN

  • For some strange reason it doesn't work, this is my piece of code:

    if CurrentTouch.state == BEGAN and onCircle (100,100,CurrentTouch.x,CurrentTouch.y, 50) then
        print ("yes")
        i = i + 1
    
    else 
        print ("not touched")
    end
    

    The "onCircle" function just makes sure I pressed the button and not something else.

  • edited November 2014 Posts: 342

    CurrentTouch's state will stay "BEGAN" until it is moved or ended. So basically, if you touch and leave your finger there without moving, it will be counted as began for as many frames as your finger stays there. Instead, use the touched function, which is only called when there is a touch EVENT, instead of remaining constant.

    function touched(t)
         if t.state == BEGAN and onCircle(100,100,t.x,t.y,50) then
              print("yes")
              i = i+1
         else
              print("not touched")
         end
    end
    
  • Thank you, this helped a lot.

  • You're welcome, any time!

  • Apparently, I still fail to understand how the function works"
    This is my "main" tab

    function setup()
        buttons = {}
        table.insert (buttons, OvalButton())
    end
    
    function draw()
        background(40, 40, 50)
    
        buttons[1]:draw()
        buttons[1]:touched(CurrentTouch)
    end
    

    This is my class

    function OvalButton:init()
        self.i = 0
    end
    
    function OvalButton:draw()
        ellipse (WIDTH/2, HEIGHT/2, 100,100)
    end
    
    function OvalButton:touched(touched)
        if touch.state == BEGAN then
            print (self.i)
            self.i = self.i + 1
        end
    end
    

    It still reacts several times, when I press once.

  • Posts: 688

    The problem is your still passing CurrentTouch to your oval button class, what you should be doing is adding the touched function to you main tab like this...

    function setup()
        buttons = {}
        table.insert (buttons, OvalButton())
    end
    
    function draw()
        background(40, 40, 50)
    
        buttons[1]:draw()
    end
    
    function touched(t)
        buttons[1]:touched(t)
    end
    
  • Thank for the help so far:)

    Now I've run into a different kind of problem. How does Codea handle multitouch? I mean my program is run step by step, there are no parallel processes, how can Codea, then, deal with a number of touches simultaneously?

    My problem is this:
    I have a space ship
    When the left button is pressed - activate the left engine
    When the right one is pressed - activate the right one

    I can handle this problem with a single touch, but I fail to understand how to deal with it if I want to implement multitouch? Say, when I press both buttons, I want both engines to run.

  • dave1707dave1707 Mod
    edited November 2014 Posts: 7,605

    @alexNaumov That's where the touch ID is used. Run this code and touch the left or right side of the screen.


    displayMode(FULLSCREEN) function setup() end function draw() background(40, 40, 50) if leftFlame then sprite("Tyrian Remastered:Flame 2",200,HEIGHT/2,50,-300) end if rightFlame then sprite("Tyrian Remastered:Flame 2",WIDTH-200,HEIGHT/2,50,-300) end end function touched(t) if t.state==BEGAN then if t.x<WIDTH/2 then leftId=t.id leftFlame=true else rightId=t.id rightFlame=true end end if t.state==ENDED then if t.id==leftId then leftFlame=false end if t.id==rightId then rightFlame=false end end end
  • @alexNaumov You should use the touched function thats automaticlly called whenever the screen is touched instead of using CurrentTouch in your draw loop. Since CurrentTouch always is the last touch even if the screen isnt being touched that can cause big problems.

    Putt the function in your main tab and then pass the touch to your buttons. It only runs when the screen is touched so it saves some power.

    function touched(touch)
    
    end
    
Sign In or Register to comment.