local function rotateBox(event) if event.phase == "began" then event.target.rotation = event.target.rotation + 90 end return true end box:addEventListener("touch", rotateBox) -- change this -- ex: crateA:addEventListener("touch", rotateBox) -- Add one to crate b (if required) crateB:addEventListener("touch", rotateBox)
As stated in #1 in my previous post, you prefix the addEventListener call with the object that you want to listen/receive touch events for.
I’ve edited the example above to show you how to add listeners to more than one object.
Just note that if you add the event listener to 3 objects (as in the example) then all the objects will rotate upon touch.
You should always reference the object in the function by event.target rather than its variable name (where appropriate), as it is good programming practise, and avoids some pitfalls. In this case, event.target is the object you attached the listener to via addEventListener()
To get around this, you can either:
A) Add different functions for each crate
B ) Give each object a .name property so you can check the objects name in the touch handler, to give it specific actions upon touch.
Here is an example of option B
local function rotateBox(event) if event.phase == "began" then if event.target.name == "crateA" then event.target.rotation = event.target.rotation + 90 end end return true end box:addEventListener("touch", rotateBox) -- change this -- ex: crateA.name = "crateA" crateA:addEventListener("touch", rotateBox) -- Add one to crate b (if required) crateB.name = "crateB" crateB:addEventListener("touch", rotateBox)
Hope this helps
PS: I strongly recommend you read through this free book: https://www.lua.org/pil/contents.html#P1
It is written by the creator of Lua, and will teach you a lot about the programming language.