The following code tries to detect contact between a yellow square, which moves accompanying our finger, and another 4 squares that are fixed. The idea is that in case of contact can be displayed in a text field the number of the object, so I’m making contact and when there is no contact appears “No contact”.
I have done several tests and appears a little problem with if … then and else.
This works, but when I add the else and it does not.
Objeto1=display.newRect (100,50,25,25) Objeto1:setFillColor (1,0,0) Objeto2=display.newRect (200,150,25,25) Objeto2:setFillColor (0,1,0) Objeto3=display.newRect (300,250,25,25) Objeto3:setFillColor (1,0,1) Objeto4=display.newRect (400,350,25,25) Objeto4:setFillColor (0,1,1) Objeto5=display.newRect (500,450,25,25) Objeto5:setFillColor (1,0,0) opcionesinfo={ text="", x=650, y=300, width=300, height=300, font= "Droid Sans Mono", fontSize=24, align= "center", } info= display.newText(opcionesinfo) info :setTextColor(1,0,0) Objetos={Objeto1.x,Objeto2.x,Objeto3.x,Objeto4.x,Objeto5.x} Objetos2={Objeto1.y,Objeto2.y,Objeto3.y,Objeto4.y,Objeto5.y} local circle = display.newRect( 50, 50, 25,25 ) circle:setFillColor( 1, 1, 0 ) local function moveCircle( event ) circle.x = event.x circle.y = event.y for barrer=1,5 do if ((math.abs (circle.x-(Objetos[barrer]))\<25) and (math.abs (circle.y-(Objetos2[barrer]))\<25)) then info.text= barrer --else --info.text= "No hay contacto" end end end Runtime:addEventListener( "touch", moveCircle )