Howdy, Stranger!

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

Is there MultiTouch?

Hi!
I am making a 2 player pong game in order to get to grips with codea, I'm trying to get user input on the touch screen, but looking online and on the codea reference. All I see is that you can get the current touch coordinates and the touch coordinates from the last frame, is there a way to get the coordinates of two separate inputs at the same time(e.g. Two fingers on the touch screen at the same time)?

Thankyou !

Comments

  • Posts: 687
    Yes. One of the built in examples is called multi touch...

    I posted a more complex example recently in this thread https://codea.io/talk/discussion/8590/joystick-code-buttons#latest
  • dave1707dave1707 Mod
    Posts: 5,996

    @rydergaz Here's a simple example of moving 2 paddles. Slide your fingers in the grey areas.

    displayMode(FULLSCREEN)
    
    function setup()
        rectMode(CENTER)
        x1,y1,x2,y2=WIDTH/2,150,WIDTH/2,HEIGHT-150
    end
    
    function draw()
        background(40, 40, 50)
        fill(255,255,255,30)
        rect(WIDTH/2,70,WIDTH,140)
        rect(WIDTH/2,HEIGHT-70,WIDTH,140)
        fill(255)
        rect(x1,y1,100,20)
        rect(x2,y2,100,20)
    end
    
    function touched(t)
        if t.state==MOVING then
            if t.y<150 then
                x1=x1+t.deltaX
            elseif t.y>HEIGHT-150 then
                x2=x2+t.deltaX
            end
        end
    end
    
  • Posts: 5

    Thanks!
    @dave1707 is it best to make the touch functions inside each of the classes for the different objects in the game ?

  • dave1707dave1707 Mod
    Posts: 5,996

    @rydergaz If you want to use classes, then you would use the touch function inside the class. There is no correct way to program because it all depends on what you want to do and how you want to do it.

  • edited August 17 Posts: 10

    @rydergaz Yes, there is a way that you can get coordinates on the same time. Here is an example:

  • edited August 17 Posts: 10
    function setup()
        print("This example tracks multiple touches and colors them based on their ID")
    
        -- keep track of our touches in this table
        touches = {}
    end
    
    -- This function gets called whenever a touch
    --  begins or changes state
    function touched(touch)
        if touch.state == ENDED then
            -- When any touch ends, remove it from
            --  our table
            touches[touch.id] = nil
        else
            -- If the touch is in any other state 
            --  (such as BEGAN) we add it to our
            --  table
            touches[touch.id] = touch
        end
    end
    
    function draw()
        background(0, 0, 0, 255)
    
        for k,touch in pairs(touches) do
    
            -- Use the touch id as the random seed
            math.randomseed(touch.id)
    
            -- This ensures the same fill color is used for the same id
            fill(math.random(255),math.random(255),math.random(255))
    
            -- Draw ellipse at touch position
            ellipse(touch.x, touch.y, 100, 100)
    
            -- Draw the id on the circle
            fill(255)
            text(touch.id, touch.x, touch.y+30)
        end
    end
    
  • Posts: 10

    Sorry you need to re-ident the code. I cant post code comments

  • dave1707dave1707 Mod
    Posts: 5,996

    @Silvio2400 Put ~~~ on a line before and after your code for it to format correctly.

  • Posts: 10

    Okay Thanks!

Sign In or Register to comment.