Referencing an Object?

             identification = “myRectangle”… self.id + “1”

             print(identification)

             identification:setFillColor(120,120,120)

               

 Hi! I am trying to use a variable to reference an object. That variable is equal to an object name such as myRectangle1, myRectangle25, and all numbers between. Why isn’t it working?

Thanks in advance, 

   BRiAN

Hey,

 

 

I don’t believe that Lua (hence Corona) allows dynamic variable names.  I’ve tried similar to what you have and had no luck.  However, you can achieve something similar but with the use of tables:

 

local id = self.id + 1

identification[“myRectangle” … id] = <object>

 

and then you can use something like this: 

 

local temp = 25

identification[“myRectangle” … temp]

 

to refer to the object at reference identification[“myRectangle25”]

 

Another option is to use tables again indexing them from myRectangle[1] to myRectangle[25]

 

 

Rich

What is self?  Is this code within a function?  

This is my newest code that i am using… 

Im trying to get it to so it lights up when it is pressed. The self:setFillColor is the thing that is not working.

[lua]

    function changeColor(event, self)

        if (event.phase == “began”) then

        

        

            self:setFillColor(250,0,0)

        

        end

    end

    

    

    rectangles = {}

    

    

    for i = 1, 25 do 

        rectangles[i] = display.newRect(i * 30, i * 30,20,20)

        if (i < 6) then

               rectangles[i].y = 100

        end

        if (i > 5) and (i < 11) then

            rectangles[i].y = 150

        end

        if (i >10) and (i <16) then

            rectangles[i].y = 200

        end

        if (i >15) and (i <21) then

            rectangles[i].y = 200

        end

        if (i >20) and (i <26) then

            rectangles[i].y = 250

        end

       

        if ( i % 5 == 0) then

            rectangles[i].x = 50

        elseif (i % 5 == 1) then

            rectangles[i].x = 100

        elseif (i % 5 == 2) then

            rectangles[i].x = 150

        elseif ( i % 5 == 3) then

            rectangles[i].x = 200

        elseif (i % 5 == 4) then

            rectangles[i].x = 250

        end

        

        rectangles[i]:addEventListener(“touch”, changeColor)

    end

    

[/lua]

This updated code should work.  You might also look at this for some other options.

You were using the self reference, but it was not set up properly.

In any case, just using the event target in the changeColor method is enough, as shown below.

[lua]

function changeColor( event )

    if (event.phase == “began”) then

        event.target:setFillColor(250,0,0)

    end

end

    

    

rectangles = {}

for i = 1, 25 do 

    rectangles[i] = display.newRect(i * 30, i * 30,20,20)

    if (i < 6) then

           rectangles[i].y = 100

    end

    if (i > 5) and (i < 11) then

        rectangles[i].y = 150

    end

    if (i >10) and (i <16) then

        rectangles[i].y = 200

    end

    if (i >15) and (i <21) then

        rectangles[i].y = 200

    end

    if (i >20) and (i <26) then

        rectangles[i].y = 250

    end

   

    if ( i % 5 == 0) then

        rectangles[i].x = 50

    elseif (i % 5 == 1) then

        rectangles[i].x = 100

    elseif (i % 5 == 2) then

        rectangles[i].x = 150

    elseif ( i % 5 == 3) then

        rectangles[i].x = 200

    elseif (i % 5 == 4) then

        rectangles[i].x = 250

    end

    

    rectangles[i]:addEventListener( “touch”, changeColor )

end

[/lua]

Hope that helps.

Hey,

 

 

I don’t believe that Lua (hence Corona) allows dynamic variable names.  I’ve tried similar to what you have and had no luck.  However, you can achieve something similar but with the use of tables:

 

local id = self.id + 1

identification[“myRectangle” … id] = <object>

 

and then you can use something like this: 

 

local temp = 25

identification[“myRectangle” … temp]

 

to refer to the object at reference identification[“myRectangle25”]

 

Another option is to use tables again indexing them from myRectangle[1] to myRectangle[25]

 

 

Rich

What is self?  Is this code within a function?  

This is my newest code that i am using… 

Im trying to get it to so it lights up when it is pressed. The self:setFillColor is the thing that is not working.

[lua]

    function changeColor(event, self)

        if (event.phase == “began”) then

        

        

            self:setFillColor(250,0,0)

        

        end

    end

    

    

    rectangles = {}

    

    

    for i = 1, 25 do 

        rectangles[i] = display.newRect(i * 30, i * 30,20,20)

        if (i < 6) then

               rectangles[i].y = 100

        end

        if (i > 5) and (i < 11) then

            rectangles[i].y = 150

        end

        if (i >10) and (i <16) then

            rectangles[i].y = 200

        end

        if (i >15) and (i <21) then

            rectangles[i].y = 200

        end

        if (i >20) and (i <26) then

            rectangles[i].y = 250

        end

       

        if ( i % 5 == 0) then

            rectangles[i].x = 50

        elseif (i % 5 == 1) then

            rectangles[i].x = 100

        elseif (i % 5 == 2) then

            rectangles[i].x = 150

        elseif ( i % 5 == 3) then

            rectangles[i].x = 200

        elseif (i % 5 == 4) then

            rectangles[i].x = 250

        end

        

        rectangles[i]:addEventListener(“touch”, changeColor)

    end

    

[/lua]

This updated code should work.  You might also look at this for some other options.

You were using the self reference, but it was not set up properly.

In any case, just using the event target in the changeColor method is enough, as shown below.

[lua]

function changeColor( event )

    if (event.phase == “began”) then

        event.target:setFillColor(250,0,0)

    end

end

    

    

rectangles = {}

for i = 1, 25 do 

    rectangles[i] = display.newRect(i * 30, i * 30,20,20)

    if (i < 6) then

           rectangles[i].y = 100

    end

    if (i > 5) and (i < 11) then

        rectangles[i].y = 150

    end

    if (i >10) and (i <16) then

        rectangles[i].y = 200

    end

    if (i >15) and (i <21) then

        rectangles[i].y = 200

    end

    if (i >20) and (i <26) then

        rectangles[i].y = 250

    end

   

    if ( i % 5 == 0) then

        rectangles[i].x = 50

    elseif (i % 5 == 1) then

        rectangles[i].x = 100

    elseif (i % 5 == 2) then

        rectangles[i].x = 150

    elseif ( i % 5 == 3) then

        rectangles[i].x = 200

    elseif (i % 5 == 4) then

        rectangles[i].x = 250

    end

    

    rectangles[i]:addEventListener( “touch”, changeColor )

end

[/lua]

Hope that helps.