Determine tapped object on tap event

I create a standard rectangle object. I add a listener to that object. The listener runs a function. The function get the number of taps, the x and y coordinates, and the event name (“tap”). I can’t figure out how to get which object was tapped.

I would like to figure out which object was tapped so that I can change the physics properties on that object (or the stroke width, color, whatever).

Here is some trimmed down code:

[blockcode]
–Text field to test values from tap event
local testTapEvent = display.newText(“Initial value”, 0, 0, native.systemFont, 32)
testTapEvent:setTextColor(255, 255, 255)
–Function that runs when the object is tapped.
local function testTap(event)

–I am able to get the event name, numtaps, etc without issue.
testTapEvent.text=event.name

–What I would like to do (actually, I want to change the physics applied to the body to static, but think I can get that once I can get ahold of the object… but if you know that as well, please add.)
–getobjectthatwastapped.setFillColor(255,0,0)

end

–Creation of rectangle
local myRectangle = display.newRect(0, 0, 150, 50)
myRectangle.strokeWidth = 3
myRectangle:setFillColor(140, 140, 140)
myRectangle:setStrokeColor(180, 180, 180)
myRectangle:addEventListener( “tap”, testTap )
[/blockcode]

Thanks for any help. [import]uid: 59514 topic_id: 10132 reply_id: 310132[/import]

Also, as a follow-up question… can additional parameters be passed through on event listeners?

Example:

Change something like this:

block:addEventListener( “touch”, dragBody ) – make object draggable

to something like this:

block:addEventListener( “touch”, dragBody(“parameter1”,“parameter2”) ) – make object draggable

I have tried different variations with no success. Thanks again.

-Kevin [import]uid: 59514 topic_id: 10132 reply_id: 36955[/import]

The object that was tapped is the “target”, so event.target i.e.

  
local function testTap(event)  
  
 event.target.alpha = 0.5  
  
end  
  

As to the second question, no you can’t do it that way. You will want to look into closures. There was a post on the blog about them but I can’t find it right now. [import]uid: 5833 topic_id: 10132 reply_id: 36971[/import]

Kevin,

when you create the rectangle you can assign any property (or method) to it

rectangle.id="myid"

in the listener function now you can check which object was tapped, using

if event.target.id=="myid" then ...

[import]uid: 6459 topic_id: 10132 reply_id: 36972[/import]

I think the target answer will help. Assigning the id would be helpful, but I will be calling the tap function on numerous auto generated objects. The target should allow me to modify that object without needing to explicitly know what it is. Thanks you both for the responses.

-Kevin [import]uid: 59514 topic_id: 10132 reply_id: 36989[/import]

Thanks… I’ll give it a try today. [import]uid: 59514 topic_id: 10132 reply_id: 36990[/import]