Problems with multi-touch[CLOSED]

Hi. I am new to Codea and am trying to make my game to have multi touch but cannot figure it out. I am trying to have to first touch in charge of moving the player and the second touch handle shooting. How do I go about doing this? I can post my code if neccesary but I am away from my ipad at the moment. Thanks in advance!

It may be difficult for players to get their touches in the right order all the time. Perhaps the conventional solution of on screen joysticks may be better.

But we’d need code to see what your problem is.

Have you looked at the multitouch example? That should provide the answers, but if you have, try putting in print statements instead of actions, just to see if its the touch code or the drawing code

@compactbear

every touch has it’s own id, so you can asign these id’s into vars

a small example:

function setup()
    firstid = nil
    secondid = nil
end

function touched(touch)
    -- check if it's a new touch
    if touch.state == BEGAN then
        -- check if the firstid is already set..., if not assign the touch id to it
        if not firstid then
            firstid = touch.id
        -- if firstid is already set, check if the second is set already, if not, assign id to it
        elseif not secondid then
            secondid = touch.id
        end
    end

    -- check if a touch has ended
    if touch.state == BEGAN then
        -- check if the firstid equals the ended touch id, if it is, then make the firstid nil again
        if firstid == touch.id then
            firstid = nil
        -- if secondid equals the ended touch id, if it is, then make the secondid nil again
        elseif secondid == touch.id then
            secondid = nil
        end
    end

end

not that this only sets some vars, nothing visual so far…

This looks great! I’ll try the code but how do I do the on screen joystick?

@compactbear, just search for joystick in the search bar, that will give you some nice examples

Thanks for all the help! I wasn’t expecting such quick responses

I always feel a wee bit sad when people suggest on screen joysticks on touch screens :frowning:

Here’s my take on a possible alternative. First touch sets the facing of the sprite. While holding the first touch a second will give you thrust in that direction. A tap will fire.

It may not be exactly what you are after and is not the best commented for beginners


-- touchcontrol

-- Use this function to perform your initial setup
function setup()
    displayMode(FULLSCREEN)
    touches={}
    tsup={} --supplemental touch info
    activetouches=0
    spacedust={}
    ship=physics.body(CIRCLE,10)
    ship.x=WIDTH/2
    ship.y=HEIGHT/2
    ship.gravityScale=0
    ship.linearDamping=0.3
    shipangle=0
    shipoffset=-90
    shipsize=30
    --bullets
    bullet={}
    bulletspeed=5
    scale=1
    globalx=0
    globaly=0
    bgx=WIDTH/2
    bgy=HEIGHT/2
end

function draw()
    globalx=ship.x
    globaly=ship.y
    background(40, 40, 50)
    activetouches=0
    oldesttouchid=0
    longesttouch=0
    for k,touch in pairs(touches) do
        activetouches = activetouches + 1
        tsup[touch.id].time = tsup[touch.id].time + 1
        if tsup[touch.id].time>longesttouch then
            longesttouch=tsup[touch.id].time
            oldesttouchid=touch.id
            shipangle=math.deg(math.atan2(touch.y-ship.y,touch.x-ship.x))
        end
    end
    
    if activetouches==2 then
        local thrust=2
        ship:applyForce(vec2(thrust*math.cos(math.rad(shipangle)),thrust*math.sin(math.rad(shipangle))))
        table.insert(spacedust,
        {x=ship.x,y=ship.y,dir=shipangle+shipoffset+math.random(20)+170,
        fade=175+math.random(50),size=2+math.random(5),speed=5+math.random(5)})
    end
            if ship.x<0 then ship.x=WIDTH end
            if ship.x>WIDTH then ship.x=0 end
            if ship.y<0 then ship.y=HEIGHT end
            if ship.y>HEIGHT then ship.y=0 end
            for i,b in pairs(bullet) do
                sprite("Tyrian Remastered:Bullet Fire A",b.x,b.y)
                b.x = b.x + scale*bulletspeed*math.sin(math.rad(-b.a))
                b.y = b.y + scale*bulletspeed*math.cos(math.rad(-b.a))
                if b.x>WIDTH or b.x<0 or b.y>HEIGHT or b.y<0 then
                    table.remove(bullet,i)
                end
            end
            
            for s,d in pairs(spacedust) do
                --add transparency to the space dust so it fades away
                tint(240,120,150,d.fade)
                pushMatrix()
                    translate(d.x,d.y)
                    sprite("Cargo Bot:Star Filled",0,0,d.size,d.size) 
                popMatrix()
                d.x = d.x + d.speed*math.sin(math.rad(-d.dir))
                d.y = d.y + d.speed*math.cos(math.rad(-d.dir))
                d.fade = d.fade -5
                if d.fade<0 then
                    table.remove(spacedust,s)
                end
            end
            tint(255)
    pushMatrix()
        translate(ship.x,ship.y) --executed 3rd -move sprite by x along x axis and y along y axis
        rotate(shipangle) --executed 2nd - rotate sprite by 20 degrees anticlockwise
        rotate(shipoffset)
        sprite("Space Art:Part Green Hull 4",0,0,shipsize) --executed 1st - draw sprite at 0,0
    popMatrix()
end

function touched(touch)
    if touch.state == ENDED then
        if tsup[touch.id].time<15 and activetouches==1 then
            table.insert(bullet,{x=ship.x,y=ship.y,a=shipangle+shipoffset})
        end
        touches[touch.id] = nil
        tsup[touch.id]=nil
    else
        touches[touch.id] = touch
        if tsup[touch.id]==nil then
            tsup[touch.id]={time=0}
        end
    end
end

I do have one question left sorry, how would I go about making two joysticks then?

oh ill look at this! thanks

I agree with West that I don’t like on screen joysticks myself and look for alternatives, but if that’s what you want, it’s just a matter of drawing two circles inside each other, for each joystick, and managing the touches. There are quite a lot of posts on the foum if you search.

Ok! This community is actually amazing and I’d dare to say better than most other forums for programming

If anyone is still in this thread, I have to ask a final(for real this time :wink: ) question. SO I have my code so on the left side of the screen i control my player and i tap to shoot on the right. How do I do this without the second “tap” interupting the first one and stopping the player from moving?

function touched(touch)
if touch.x > WIDTH/2 then
--Shoot Code or whatever
elseif
touch.x <= WIDTH/2 then
--Move code or whatever
end
end

@CodeaNoob - what happens with your code if you touch EXACTLY on the middle of the screen? >:)

@Ignatz. Touching the exact center would be an odd thing indeed!

I edited the post to cover it now,

Sorry, I know perfectly well you will never touch the exact centre, I just couldn’t resist the tease :wink:

Lolz @Ignatz, :smiley:

@Ignatz, you sneaky mouse, always teasing everyone ;))