Have a little question... Again :(

Hello everyone who will read this, I have stumbled into another problem. I’m making a game where it’s an overhead view (like the original Zelda game) and I have to detect multiple touches in order to control the character. The way how I have it as if now is the left side of the screen is how you control which way the character walks and the right makes him/her shoot or attack. The problem is I can’t have both happen at the same time and the multi touch example provided with the app isn’t really helping too much. Plz help.

Ps. Sorry for asking too many questions lately

Sincerely,

Qtip

Can you show us some code?

yojimbo2000

function touched(t)
if t.state==ENDED then

    px=0 

    py=0

    else if t.state==BEGAN or t.state==MOVING then

    if t.x < WIDTH*.08 then

    px=1

    else if t.x > WIDTH*.15 and t.x < WIDTH*.2 then

    px=2

    else if t.x > WIDTH*.2 and t.x < WIDTH*.15 then

    px=0

            end            

        end

    end

end

if t.state==ENDED then py=0

else if t.state==BEGAN or t.state==MOVING and xpos<5000 and xpos>-5000 

then

if t.y < HEIGHT*.20 and t.x < WIDTH*.5 then 

py=1

else if t.y > HEIGHT*.30 and t.y < HEIGHT*.5 and t.x < WIDTH*.5 then 

py=2

else if t.y > HEIGHT*.20 and t.y < HEIGHT*.30 and t.x <WIDTH*.5 then 

py=0

                        end

                    end

                end

            end

        end

    end

if CurrentTouch.state==BEGAN and CurrentTouch.x < WIDTH*.2
and CurrentTouch.x >.15 then

px=0

end

end>

Oh wow that isn’t come out well at all XD @yojimbo2000

Btw I use the px and py variable to move the player

Put three tildes ~~~ at the top and bottom of code blocks you want to post in the forum


function touched(t) if t.state==ENDED then

    px=0 

    py=0

    else if t.state==BEGAN or t.state==MOVING then

    if t.x < WIDTH*.08 then

    px=1

    else if t.x > WIDTH*.15 and t.x < WIDTH*.2 then

    px=2

    else if t.x > WIDTH*.2 and t.x < WIDTH*.15 then

    px=0

            end            

        end

    end

end

if t.state==ENDED then py=0

else if t.state==BEGAN or t.state==MOVING and xpos<5000 and xpos>-5000 
then

if t.y < HEIGHT*.20 and t.x < WIDTH*.5 then 

py=1

else if t.y > HEIGHT*.30 and t.y < HEIGHT*.5 and t.x < WIDTH*.5 then 

py=2

else if t.y > HEIGHT*.20 and t.y < HEIGHT*.30 and t.x <WIDTH*.5 then 

py=0

                        end

                    end

                end

            end

        end

    end
if CurrentTouch.state==BEGAN and CurrentTouch.x < WIDTH*.2 and CurrentTouch.x >.15 then

px=0

end
end

That’s my touch detection @yojimbo2000

@Qtipit Here’s an example I wrote for someone awhile ago. One of the direction buttons and the shoot button can be pressed at the same time.

function setup()
    rectMode(CENTER)
    dir=""
    stab=""
    btn={}
    table.insert(btn,buttons(300,300,100,50,"UP"))
    table.insert(btn,buttons(300,100,100,50,"DOWN"))
    table.insert(btn,buttons(200,200,100,50,"LEFT"))
    table.insert(btn,buttons(400,200,100,50,"RIGHT"))
    table.insert(btn,buttons(300,400,100,50,"SHOOT"))
end

function draw()
    background(40, 40, 50)
    for a,b in pairs(btn) do
        b:draw()
        if b.pressed then
            if b.text=="SHOOT" then
                stab=b.text
            elseif dir=="" then
                dir=b.text
            end
        end
    end
    fill(255)
    text(dir,WIDTH/2,HEIGHT-50)
    text(stab,WIDTH/2,HEIGHT-100)
end

function touched(t)
    for a,b in pairs(btn) do
        b:touched(t)
    end
end

buttons=class()

function buttons:init(x,y,w,h,txt)
    self.x=x
    self.y=y
    self.w=w
    self.h=h
    self.text=txt
    self.left=x-w/2
    self.right=x+w/2
    self.bottom=y-h/2
    self.top=y+h/2
    self.pressed=false  
    self.id=0 
end

function buttons:draw()
    fill(255)
    if self.pressed then
        fill(255, 158, 0, 255)
    end
    rect(self.x,self.y,self.w,self.h)   
    fill(255,0,0) 
    text(self.text,self.x,self.y)
end

function buttons:touched(t)
    if t.state==BEGAN or t.state==MOVING and self.id==0 then
        if t.x>self.left and t.x<self.right and 
                    t.y>self.bottom and t.y<self.top then
            self.pressed=true
            self.id=t.id
        end
    end
    if t.state==MOVING and self.id==t.id then
        if t.x>self.left and t.x<self.right and 
                    t.y>self.bottom and t.y<self.top then
            self.pressed=true
        else
            self.pressed=false
            self.id=0
            dir=""
            stab=""
        end
    end
    if t.state==ENDED and self.id==t.id then
        self.pressed=false
        self.id=0
        dir=""
        stab=""
    end
end

Hey thanks a lot @dave1707 I think I get it I’ll try and implement it into my code :smiley:

@Qtipit - please try to make your code look like Dave’s, ie indent it for loops, if statements, take out the blank lines, etc.

You will find it much easier to read and debug.

@Qtipit yeah, you have more ends than you do block opening statements (that’s why your code isn’t indented properly, and probably doesn’t run). else if isn’t the same as elseif (without the space. You probably want elseif ). If you’re using a touched function, don’t use CurrentTouch. I recommend you look at @Ignatz 's eBook on Lua, you can find it at coolcodea.wordpress.com

Ok I’ll try @yojimbo2000 and @ignatz I appreciate the tips