As I Access the color of an object within a table

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
    
    – Next line is wrong
    --(Objetos[barrer]):setFillColor (1,1,0)
    
   info.text= barrer
    break
else
   info.text= “No hay contacto”
    end
end
end

Hi @fferraro67,

You haven’t indicated the specific error you’re getting, but I assume it’s because you’re incorrectly including parentheses:

[lua]

– WRONG

(Objetos[barrer]):setFillColor (1,1,0)

– CORRECT

Objetos[barrer]:setFillColor (1,1,0)

[/lua]

Of course, this assumes that “Objetos[barrer]” references a display object, and not something else.

Brent

The idea is to expand a little initial reference: when the yellow square touches other objects, they take their color.

This is the complete code with your erroneous line:

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 &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; 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 Runtime:addEventListener( "touch", moveCircle )

Hi @fferraro67,

I see that your table “Objetos” does not contain display objects. Instead, it contains the X position of the objects you declared above. So, in this code, you’re trying to set the fill color of a number, which is why it’s malfunctioning.

Take care,

Brent

How indexed color of each object within a table? Likewise do I do outside?

Example:

Objetos3={Objeto1:setFillColor(1,0,1),Objeto2:setFillColor(0,1,1),Objeto3:setFillColor(1,0,1),Objeto4:setFillColor(1,0,0),Objeto5:setFillColor(1,0,0)}

Hi @fferraro67,

The better approach would be to place the actual objects (references) inside the “Objetos” table.

[lua]

local Objetos={ Objeto1, Objeto2, Objeto3, Objeto4, Objeto5 }

[/lua]

That way, you have access to all of the properties, and you could also apply fill colors in that way.

[lua]

Objetos[barrer]:setFillColor (1,1,0)

[/lua]

Or you could check their X and Y coordinates:

[lua]

if ( ( math.abs ( circle.x - ( Objetos[barrer].x ) ) < 25 ) and

[/lua]

Brent

Thank you, Brent. Works perfect. I did not know you could do so. What link I can learn more about using tables in LUA?

Hi @fferraro67,

These two resources should help:

http://docs.coronalabs.com/guide/start/introLua/index.html#tables

http://lua-users.org/wiki/TablesTutorial

Brent

Hi @fferraro67,

You haven’t indicated the specific error you’re getting, but I assume it’s because you’re incorrectly including parentheses:

[lua]

– WRONG

(Objetos[barrer]):setFillColor (1,1,0)

– CORRECT

Objetos[barrer]:setFillColor (1,1,0)

[/lua]

Of course, this assumes that “Objetos[barrer]” references a display object, and not something else.

Brent

The idea is to expand a little initial reference: when the yellow square touches other objects, they take their color.

This is the complete code with your erroneous line:

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 &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; 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 Runtime:addEventListener( "touch", moveCircle )

Hi @fferraro67,

I see that your table “Objetos” does not contain display objects. Instead, it contains the X position of the objects you declared above. So, in this code, you’re trying to set the fill color of a number, which is why it’s malfunctioning.

Take care,

Brent

How indexed color of each object within a table? Likewise do I do outside?

Example:

Objetos3={Objeto1:setFillColor(1,0,1),Objeto2:setFillColor(0,1,1),Objeto3:setFillColor(1,0,1),Objeto4:setFillColor(1,0,0),Objeto5:setFillColor(1,0,0)}

Hi @fferraro67,

The better approach would be to place the actual objects (references) inside the “Objetos” table.

[lua]

local Objetos={ Objeto1, Objeto2, Objeto3, Objeto4, Objeto5 }

[/lua]

That way, you have access to all of the properties, and you could also apply fill colors in that way.

[lua]

Objetos[barrer]:setFillColor (1,1,0)

[/lua]

Or you could check their X and Y coordinates:

[lua]

if ( ( math.abs ( circle.x - ( Objetos[barrer].x ) ) < 25 ) and

[/lua]

Brent

Thank you, Brent. Works perfect. I did not know you could do so. What link I can learn more about using tables in LUA?

Hi @fferraro67,

These two resources should help:

http://docs.coronalabs.com/guide/start/introLua/index.html#tables

http://lua-users.org/wiki/TablesTutorial

Brent