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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
--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 ) 

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

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

1
2
3
4
5
local function testTap(event)
 
     event.target.alpha = 0.5
 
end

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 ...

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

Thanks.. I'll give it a try today.

views:2034 update:2011/10/1 9:04:19
corona forums © 2003-2011