Tables

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 ) &nbsp; &nbsp; circle.x = event.x &nbsp; &nbsp; 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 &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;info.text= barrer --else --info.text= "No hay contacto" &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; end end end Runtime:addEventListener( "touch", moveCircle )

The problem is the way you’re looping through the list of blocks.

If you  drag your circle over #1, it checks #1 and it finds contact, then it checks #2 and it’s not in contact, it checks #3, no contact etc.  When you drag it on #5, it will show #5.

What you can do is put a “break” statement inside the if portion, so it will escape your loop.

if ((math.abs (circle.x-(Objetos[barrer]))\<25) and (math.abs (circle.y-(Objetos2[barrer]))\<25)) then &nbsp;&nbsp; info.text= barrer &nbsp;&nbsp; break else &nbsp;&nbsp; info.text= "No hay contacto" &nbsp;&nbsp;&nbsp; end end end

Rob

Rob Thanks for the advice. Excellent solution. Is there any way to make it easier and less programming lines. Now there are 5 objects but could be 100.

You’re moveCircle is pretty efficient once you get the break in.  Unless you have a solid programatic way to fill out your parameters for your rectangles, you’re pretty much going to have to write code for all of it.

Though the best practice would be to create an external .lua file as a module, say “blocks.lua”:

local m = {}

m[1] = {}

m[1].x = 10

m[1].y = 20

m[1].color = {1, 0, 0}

m[2] = {}

m[2].x = 20

m[2].y = 40

m[2].color = {1, 1, 0}

etc.

Then you could do:

local blocks = require(“blocks”)

local objects = {}

for i = 1, #blocks do

     objects = display.newRect(…)

     …

end

As I Access the color of an object within a table. The idea is that objects take the color of the touch object, which in this case is yellow.

I tried this but it does not work. I could not find a way to access the color, but within the table.

for barrer=1,5 do if ((math.abs (circle.x-(Objetos[barrer]))\<25) and (math.abs (circle.y-(Objetos2[barrer]))\<25)) then &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;-- Next line is wrong &nbsp; &nbsp; --(Objetos[barrer]):setFillColor (1,1,0) &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;info.text= barrer &nbsp; &nbsp; break else &nbsp; &nbsp;info.text= "No hay contacto" &nbsp; &nbsp; end end end

The problem is the way you’re looping through the list of blocks.

If you  drag your circle over #1, it checks #1 and it finds contact, then it checks #2 and it’s not in contact, it checks #3, no contact etc.  When you drag it on #5, it will show #5.

What you can do is put a “break” statement inside the if portion, so it will escape your loop.

if ((math.abs (circle.x-(Objetos[barrer]))\<25) and (math.abs (circle.y-(Objetos2[barrer]))\<25)) then &nbsp;&nbsp; info.text= barrer &nbsp;&nbsp; break else &nbsp;&nbsp; info.text= "No hay contacto" &nbsp;&nbsp;&nbsp; end end end

Rob

Rob Thanks for the advice. Excellent solution. Is there any way to make it easier and less programming lines. Now there are 5 objects but could be 100.

You’re moveCircle is pretty efficient once you get the break in.  Unless you have a solid programatic way to fill out your parameters for your rectangles, you’re pretty much going to have to write code for all of it.

Though the best practice would be to create an external .lua file as a module, say “blocks.lua”:

local m = {}

m[1] = {}

m[1].x = 10

m[1].y = 20

m[1].color = {1, 0, 0}

m[2] = {}

m[2].x = 20

m[2].y = 40

m[2].color = {1, 1, 0}

etc.

Then you could do:

local blocks = require(“blocks”)

local objects = {}

for i = 1, #blocks do

     objects = display.newRect(…)

     …

end

As I Access the color of an object within a table. The idea is that objects take the color of the touch object, which in this case is yellow.

I tried this but it does not work. I could not find a way to access the color, but within the table.

for barrer=1,5 do if ((math.abs (circle.x-(Objetos[barrer]))\<25) and (math.abs (circle.y-(Objetos2[barrer]))\<25)) then &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;-- Next line is wrong &nbsp; &nbsp; --(Objetos[barrer]):setFillColor (1,1,0) &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;info.text= barrer &nbsp; &nbsp; break else &nbsp; &nbsp;info.text= "No hay contacto" &nbsp; &nbsp; end end end