Colours, functions and nils, oh my!

So I’m working on what’s basically an assignment for college course and I’ve been wracking my brain trying to figure out how to solve the problem. I have a 2D array that contains nine objects which in turn hold 14 objects (is array the right term?) a grid is generated of this size (9 across, 14 down) of squares. When a grid is tapped, the colour is supposed to change. The issue I’m having is I can’t figure how to get it such that the :setFillColor works in the function without it coming up as a nil value. If someone could point me in the right direction, that’d be swell.

function tappedRect(x)     print("tapped") -- Testing function x:setFillColor (0, 255, 0) -- unsure what to type here to get the color to change. end   for i = 1, gridWidth, 1 do     grid[i] = {}     for j = 1, gridHeight, 1 do         grid[i][j] = {}              end end   for i = 1, gridWidth, 1 do     for j = 1, gridHeight, 1 do     grid[i][j].ship = false         xCoOrd =  i \* 60 -- The squares are fifty pixels wide. This creates the grid with 10 pixels gap across         yCoOrd = j \* 60  -- The squares are fifty. This creates the grid with 10 pixels gap down.     grid[i][j].squares = display.newRect(xCoOrd, yCoOrd, 50, 50)    -- grid[i][j].squares saves the memory of the squares     grid[i][j].squares:setFillColor(255,255,255)     grid[i][j].squares:addEventListener("tap", tappedRect)     end end

The parameter passed to the tappedRect function (called “x” in your code), does not refer to the object that has been tapped, but the tap event itself. This is why in most sample code you will see the parameter is called “event” or “e”.

Luckily, the tap event does have a property called “target” which refers to the object that was tapped.

Simply change your setFillColor line to :

x.target:setFillColor (0, 255, 0)

On a separate note, if you are using a recent corona build which uses graphics 2.0 (anything from build 2076 I think) the color range now goes from 0-1 instead of 0-255. So your function should actually use the values (0, 1, 0).

The parameter passed to the tappedRect function (called “x” in your code), does not refer to the object that has been tapped, but the tap event itself. This is why in most sample code you will see the parameter is called “event” or “e”.

Luckily, the tap event does have a property called “target” which refers to the object that was tapped.

Simply change your setFillColor line to :

x.target:setFillColor (0, 255, 0)

On a separate note, if you are using a recent corona build which uses graphics 2.0 (anything from build 2076 I think) the color range now goes from 0-1 instead of 0-255. So your function should actually use the values (0, 1, 0).