I’m trying to reuse event listeners here to minimize/simply the code. Issue is I’m still stuck with have to do if/then loops in the listener (see code). So questions are:
Q1. Is there way to assign a variable by reference in lua? (e.g. a = b, but such that it will be by reference)
Q2. If not any other ideas re how to simply the code in the listener below? Note imagine that there were more variables involved (e.g. say 10 lock objects), and for each one the number of steps you wanted to perform were greater. That is the overhead of not being able to “pass-by-reference” is then quite high in terms of duplicate code.
[code]
– Variables
local lock1Text, lock2Text
local lock1Value = 1
local lock2Value = 1
function createLock(parentGroup, x, y, name)
local newLockTextObj = display.newText("", 0, 0, native.systemFont, 40)
newLockTextObj.name = name
return newLockTextObj
end
lock1Text = createLock(lockGroup, 50, 100, “lock1”)
lock2Text = createLock(lockGroup, 100, 100, “lock2”)
lock1Text:addEventListener(“touch”, lock1TextTouchListener)
lock2Text:addEventListener(“touch”, lock1TextTouchListener)
local function lock1TextTouchListener(event)
if event.phase == “began” then
– QUESTION HOW TO AVOID HAVING TO RUN THOUGH IF’S HERE
if event.target.name == “lock1” then
lock1Value = lock1Value + 1
if lock1Value == 10 then lock1Value = 1 end
elseif event.target.name == “lock2” then
lock2Value = lock2Value + 1
if lock2Value == 10 then lock2Value = 1 end
end
end
return true
end
[/code] [import]uid: 140210 topic_id: 27999 reply_id: 327999[/import]