is there a way to pack extra info in the touch-event table?

hi there!

I’m hitting an obstacle here: I have six disks on screen, and users can touch and rotate these discs (by moving there finger around once they have started their touch on one of these discs).

I’m using multitouch, and the problem is that I want to know which disc needs to stop spinning when a certain touch ends. It would be great if at the start of a touch event (using multitouch, by the way) I could say something like “if event.phase == “began” then event.discTouched = 3” but unfortunately the event parameter is local to that if…then statement, so at the event.phase == “ended” point the variable event.discTouched is nil!

For complicated reasons I can’t use the event.target parameter, so unfortunately this won’t help, and I’m really in the situation where I would like to “stuff” extra info in the touch event table myself. If I don’t do this I will need to do a for-loop over all of my active touch-events which would slow down my code and seems like crappy logic.

So, does anybody know if this is possible somehow? Logically, if I could declare this variable ( let’s say ‘event.extraInfo’ ) at the start of my function that handles touch events, it would be in scope for everything in this function, but there is no way to declare it without setting it, and if I set it I always overwrite the data in it, which is obviously of no use.

cheers,
Thomas

p.s. It’s been a long time since I was on this forum - after 6 months of no time to code I finally started work on my second game app. It’s good to be back! [import]uid: 70134 topic_id: 34034 reply_id: 334034[/import]

To clear things up a bit, here’s some super-simple code that illustrates the problem. If you try this out you will see that the terminal prints nil all the time, because the variable event.extraInfo is out of scope.

[lua]local screenTouched = function(event)

if event.phase == “began” then
event.extraInfo = math.random(999)
elseif event.phase == “moved” then
print(event.extraInfo)
end

end – screenTouched

Runtime:addEventListener(“touch”, screenTouched)[/lua] [import]uid: 70134 topic_id: 34034 reply_id: 135345[/import]

local function wrapParam(funcPtr, param)  
 return function(...)  
 return funcPtr(param, ...)  
 end  
end  

What it does is assign a return value to a function call in advance of the call itself.

Use case:

local screenTouched = function(data, event)  
  
 if event.phase == "began" then  
 data.randomval = math.random(999)  
 elseif event.phase == "moved" then  
 print(data.randomval)  
 end  
   
end -- screenTouched  
  
local data = {}  
local touchFunc = wrapParam(screenTouched, data) --data is now the first parameter of touchFunc, but otherwise is the screenTouched function  
Runtime:addEventListener("touch", touchFunc)  

There are a number of other uses for wrapParam, and if you need, say a tableView render and tableView update to have a connected table of references between the two, it also works as you can wrapParam the same table to both functions and then you have a channel for communication between the two.

The downside is it makes removeEventListener a bit of a pain as screenTouched and touchFunc are different references, and so if you plan on removing the eventListener you need to keep the wrapped function around somewhere to reference. [import]uid: 134101 topic_id: 34034 reply_id: 135349[/import]

To clear things up a bit, here’s some super-simple code that illustrates the problem. If you try this out you will see that the terminal prints nil all the time, because the variable event.extraInfo is out of scope.

[lua]local screenTouched = function(event)

if event.phase == “began” then
event.extraInfo = math.random(999)
elseif event.phase == “moved” then
print(event.extraInfo)
end

end – screenTouched

Runtime:addEventListener(“touch”, screenTouched)[/lua] [import]uid: 70134 topic_id: 34034 reply_id: 135345[/import]

local function wrapParam(funcPtr, param)  
 return function(...)  
 return funcPtr(param, ...)  
 end  
end  

What it does is assign a return value to a function call in advance of the call itself.

Use case:

local screenTouched = function(data, event)  
  
 if event.phase == "began" then  
 data.randomval = math.random(999)  
 elseif event.phase == "moved" then  
 print(data.randomval)  
 end  
   
end -- screenTouched  
  
local data = {}  
local touchFunc = wrapParam(screenTouched, data) --data is now the first parameter of touchFunc, but otherwise is the screenTouched function  
Runtime:addEventListener("touch", touchFunc)  

There are a number of other uses for wrapParam, and if you need, say a tableView render and tableView update to have a connected table of references between the two, it also works as you can wrapParam the same table to both functions and then you have a channel for communication between the two.

The downside is it makes removeEventListener a bit of a pain as screenTouched and touchFunc are different references, and so if you plan on removing the eventListener you need to keep the wrapped function around somewhere to reference. [import]uid: 134101 topic_id: 34034 reply_id: 135349[/import]