I’m trying to add diagonal controls to my D-Pad. I already have the Up,Down,Left,Right on screen controlling a sprite but I want to implement diagonal top right, diagonal top left , diagonal bottom right and diagonal bottom left. I’ll abbreviate to DTR, DTL, DBR and DBL.
I noticed if I touch Up and swipe my finger to Left it will make the sprite move DTL. Same with touch Up swipe Right moves DTR but it doesn’t work if I touch Left or Right then swipe Top. Vice versa with Down swipe Right and Down swipe Left. You’ll get a better understanding by seeing. The codes below, basically I want a one touch button for each diagonal movement.
function setup()
--run in landscape mode
displayMode(FULLSCREEN)
rectMode(CENTER)
b1=vec4(WIDTH/2-70,150,100,50) --button 1 left
b2=vec4(WIDTH/2+70,150,100,50) --button 2 right
b3=vec4(WIDTH/2,220,100,50) --button 3 top
b4=vec4(WIDTH/2,80,100,50) --button 4 bottom
b5=vec4(WIDTH/2-85,HEIGHT*.28,50,50) --button 5 diagonal top left
b6=vec4(WIDTH/2+85,HEIGHT*.28,50,50) --button 6 diagonal top right
b7=vec4(WIDTH/2-85,HEIGHT*.11,50,50) --button 7 diagonal bottom left
b8=vec4(WIDTH/2+85,HEIGHT*.11,50,50) --button 8 diagonal bottom right
sx,sy=WIDTH/2,HEIGHT/2 -- sprite x,y
xs=0 -- x speed
xs2=0
end
function draw()
background(40, 40, 50)
fill(255)
rect(b1.x,b1.y,b1.z,b1.w) -- button 1 left
rect(b2.x,b2.y,b2.z,b2.w) -- button 2 right
rect(b3.x,b3.y,b3.z,b3.w) -- button 3 top
rect(b4.x,b4.y,b4.z,b4.w) -- button 4 bottom
--Diagonal Buttons
fill(255, 179, 0)
ellipse(b5.x,b5.y,b5.z,b5.w) --button 5 diagonal top left
ellipse(b6.x,b6.y,b6.z,b6.w) --button 6 diagonal top right
ellipse(b7.x,b7.y,b7.z,b7.w) --button 7 diagonal bottom left
ellipse(b8.x,b8.y,b8.z,b8.w) --button 8 diagonal bottom right
fill(255,0,0)
text("Left",b1.x,b1.y)
text("Right",b2.x,b2.y)
text("Top",b3.x,b3.y)
text("Bottom",b4.x,b4.y)
text("DTL",b5.x,b5.y)
text("DTR",b6.x,b6.y)
text("DBL",b7.x,b7.y)
text("DBR",b8.x,b8.y)
sprite(asset.builtin.Planet_Cute.Character_Boy,sx,sy,150,150)
sx=sx+xs
sy=sy+xs2
end
function touched(t)
if t.state==BEGAN or t.state==MOVING then
xs=0
if t.x>b1.x-b1.z/2 and t.x<b1.x+b1.z/2 and
t.y>b1.y-b1.w/2 and t.y<b1.y+b1.z/2 then
xs=-2.2
end
if t.x>b2.x-b2.z/2 and t.x<b2.x+b2.z/2 and
t.y>b2.y-b2.w/2 and t.y<b2.y+b1.z/2 then
xs=2.2
end
if t.x>b3.x-b3.z/2 and t.x<b3.x+b3.z/2 and
t.y>b3.y-b3.w/2 and t.y<b3.y+b1.z/2 then
xs2=2.2
end
if t.x>b4.x-b4.z/2 and t.x<b4.x+b4.z/2 and
t.y>b4.y-b4.w/2 and t.y<b4.y+b1.z/2 then
xs2=-2.2
end
elseif t.state==ENDED then
xs=0
xs2=0
end
end