I have a single function that I want to apply to many objects.
local function targetEnemy(event) if event.phase == "ended" and activeChar ~= nil then activeChar.target = self end end
I have the event listeners to see if the objects have a touch event
enemy1:addEventListener( "touch", targetEnemy ) enemy2:addEventListener( "touch", targetEnemy )
When I use the code as it is above it will run without error, but it obviously does not change the activeChar.target to self, it remains nil
when I edit the function to the following by inserting “self”
local function targetEnemy(self, event) if event.phase == "ended" and activeChar ~= nil then activeChar.target = self print("hello") end end
I then run the program and I get the error
Attempt to index local ‘event’ (a nil value)
File: main.lua
Line: 146
I find it strange that the issue seems to be with “event” seeing as it works prior without “self” included.
Can someone help with the logic that I am missing behind this?