# How do I detect touch within a certain area?

Hi all,
Im trying to create a 3x2 slider puzzle, but I’m stuck. I have no clue how to detect if a CurrentTouch is in a certain area (in my case, on a certain rectangle). I thought of making an if statement to see if CurrentTouch.x/y is within 150/100 (my width and height), but couldn’t wrap my head around it.

The goal is to touch a rectangle, and it would switch values with a blank space. Before I make a proper switching function, I’m here to ask how to detect which rectangle I touched, but I did include a switch function to help me get a grasp on how switching will look like.

Here’s what I have so far.

``````function setup()
rectx={100,300,500,100,300,500,0} --last value 0 is placeholder
recty={500,500,500,300,300,300,0}
col={color(255, 255, 255, 255),     color(193, 193, 193, 255),        color(127, 127, 127, 255),
color(0, 0, 0, 255),     color(67, 67, 67, 255)}
end

function drawrect()
for i =1, 5 do
fill(col[i])
rect(rectx[i],recty[i],150,100)
end
end

function switch()
if CurrentTouch.state==BEGAN then --just testing switcheroo ability
rectx=rectx --stores old fifth rectangle value
recty=recty
rectx=rectx --gives new value to fifth rectangle
recty=recty
rectx=rectx --previously filled position is now the empty position
recty=recty
print(rectx)
end
end

function draw()
background(115, 184, 204, 255)
drawrect()

switch()
end
``````

Thanks.

Use the event function touched in main script.
`function touched(touch) if touch.state == BEGAN then -- your code to check if touch.x/y is in rect.x/y/width/height end end`

@Murkythunder Here’s something I had sitting around. I don’t know if you know anything about classes yet, but if you don’t understand what’s happening here, you’ll will in the future if you keep learning. Something like this might help you later in what you’re doing. For touch identification, tap a white square and it turns red. Tap a red square and it turns white.

``````supportedOrientations(LANDSCAPE_ANY)

function setup()
parameter.integer("size",2,8,4,squares)
rectMode(CENTER)
end

function squares()
sq={}
cnt=0
for x=1,size do
for y=1,size do
cnt=cnt+1
table.insert(sq,square(x*85,y*85,cnt))
end
end
end

function draw()
background(0)
for a,b in pairs(sq) do
b:draw()
end
end

function touched(t)
if t.state==BEGAN then
for a,b in pairs(sq) do
b:touched(t)
end
end
end

square=class()

function square:init(x,y,c)
self.x=x
self.y=y
self.w=80
self.h=80
self.color=color(255)
self.c=c
end

function square:draw()
fill(self.color)
rect(self.x,self.y,self.w,self.h)
fill(0)
text(self.c,self.x,self.y)
end

function square:touched(t)
if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
if self.color==color(255,0,0) then
self.color=color(255)
else
self.color=color(255,0,0)
end
end
end
``````

Thanks @Dominik I didn’t realize I could use that.
Also, thanks @dave1707 , but I haven’t a clue how classes work in lua. The reason I made this was to visualize how the algorithm for a slider puzzle solver would work, in javascript. I chose codea and lua because I knew I could make a great test puzzle to vizualize the solution process (somewhat) quickly. Here is my final code, I tried to test if you could get the blocks to appear in order: white, light grey, grey, (next line) dark grey, black FROM white, light grey, grey, (next line) black, dark grey. Conclusion: no

``````function setup()
rectx={100,300,500,100,300,500,0} --last value 0 is placeholder
recty={500,500,500,300,300,300,0}
col={color(255, 255, 255, 255),     color(193, 193, 193, 255),        color(127, 127, 127, 255),
color(0, 0, 0, 255),     color(67, 67, 67, 255)}

x=1
end

function drawrect()
for i =1, 5 do
fill(col[i])
rect(rectx[i],recty[i],150,100)
end
end

function nextto(numb)
distbetweenx=rectx-rectx[numb]
distbetweeny=recty-recty[numb]
if distbetweenx < 0 then
distbetweenx= -1 * distbetweenx
end
if distbetweeny < 0 then
distbetweeny= -1 * distbetweeny
end

if distbetweenx == 200 and distbetweeny ~= 200 then
return true

elseif distbetweeny == 200 and distbetweenx==0 then
return true
end
end

function switch(num)
--just testing switcheroo ability
rectx=rectx[num] --stores old fifth rectangle value
recty=recty[num]
rectx[num]=rectx --gives new value to fifth rectangle
recty[num]=recty
rectx=rectx --previously filled position is now the empty position
recty=recty

end

function touched(touch)
if touch.state == BEGAN then
for i =1,5 do
if touch.x < (rectx[i]+150) and touch.x > (rectx[i]) and touch.y < (recty[i]+100) and touch.y > (recty[i]) and nextto(i)
then switch(i)
end
end
end
end

function draw()
background(115, 184, 204, 255)
drawrect()
end
``````

Looking back, I probably could of figured out that this sort of puzzle is impossible to solve because you can’t rearrange the blocks drastically in a 3x2, by thinking for about ten more seconds. Instead, I built a program in an another language to solve it hands on, only to discover that their was no solution… But, oh well. It was fun 8)

PS: if it turns out it is possible to solve a randomly arranged 3x2, let me know, it’s been a long day.

@Murkythunder Solving your puzzle is like this one. Sometimes the random starting order results in an impossible solution.

``````-- puzzle15

displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)

function setup()
count = 0
size=150
done=false
ntab={}    -- number table
otab={}    -- replay table
-- table of valid moves
tab = { {2,5,},{1,3,6,},{2,4,7,},{3,8,},{1,6,9,},{2,5,7,10,},
{3,6,8,11,},{4,7,12,},{5,10,13,},{6,9,11,14,},{7,10,12,15,},
{8,11,16,},{9,14,},{10,13,15,},{11,14,16,},{12,15,} }
create16()
end

function create16()    -- create starting puzzle
for z = 1,16 do
ntab[z]=0
rnd = randomNbr()
ntab[z]=rnd
otab[z]=rnd
if rnd == 16 then
n=z     -- if 16 then this will be the blank space
end
end
ntab[n]=""    -- blank
otab[n]=""
end

function randomNbr()    -- return number not used yet
while 1 do
a = math.random(1,16)
for z = 1,16 do
if ntab[z] == 0 then
return(a)
end
if ntab[z] == a then
break
end
end
end
end

function draw()
background(40,40,50)
offset=0
rectMode(CENTER)
text("REPLAY",190,800)
text("NEW",600,800)
for y=4,1,-1 do
for x=1,4 do
offset = offset + 1
fill(200,0,0)
rect(x*size,y*size,size,size)
fill(255)
fontSize(60)
text(ntab[offset],x*size,y*size)
end
end
if done then
str=string.format("Complete   %3d",count)
text(str,WIDTH/2,HEIGHT-100)
else
str=string.format("Number of moves   %3d",count)
text(str,WIDTH/2,HEIGHT-50)
end
end

function touched(t)
if t.state==BEGAN then
-- replay game
if t.x>100 and t.x<300 and t.y>750 and t.y<850 then
for z=1,16 do
ntab[z]=otab[z]
end
count=0
done=false
end
-- new game
if t.x>500 and t.x<700 and t.y>750 and t.y<850 then
setup()
end
-- move piece
if not done then
z=0
for y=4,1,-1 do
for x=1,4 do
z = z + 1
if t.x > x*size-35 and t.x < x*size+35 then
if t.y > y*size-35 and t.y < y*size+35 then
checkblank(z)
end
end
end
end
end
end
end

function complete()    -- check for a completed game
count = count + 1
for z = 1,15 do
if ntab[z] ~= z then
return
end
end
done=true
end

function checkblank(pos)    -- check for the blank space
for z = 1,#tab[pos] do
val = tab[pos][z]
if ntab[val] == "" then
ntab[val] = ntab[pos]
ntab[pos] = ""
complete()
return
end
end
end
``````