check whether a specified rectangle / button is pressed

Hi. 

i want to crete a function to check, if a specified button is being clicked. The purpose is to store a variable

with different value based on what user clicked. So, for example there are 5 buttons. if I clicked the 1st button, 

then the variable will store ‘1’, ‘2’ for clicking the 2nd button, and so on.

is there any API for checking items like rectangles?

[lua]

local stored

local rects = {}

local touchButton = function (event)

  local obj = event.target

  if event.phase == “ended” then

      stored = obj.id

      print ("STORED NUMBER: "…stored)

  end

end

for a = 1, 5, 1 do

  local r = display.newRect(0, 0, 100, 50)

  r.x = 160

  r.y = a * 52

  r:addEventListener(“touch”,touchButton)

  r.id = a

  rects[#rects+1] = r

end

[/lua]

thanks :smiley:

[lua]

local stored

local rects = {}

local touchButton = function (event)

  local obj = event.target

  if event.phase == “ended” then

      stored = obj.id

      print ("STORED NUMBER: "…stored)

  end

end

for a = 1, 5, 1 do

  local r = display.newRect(0, 0, 100, 50)

  r.x = 160

  r.y = a * 52

  r:addEventListener(“touch”,touchButton)

  r.id = a

  rects[#rects+1] = r

end

[/lua]

thanks :smiley: