I’m not sure I understand event.target. If I run the code below, it says that event.target is nil. Will someone help me?
obj = display.newImage("image.png") local function onTap() print(event.target) end obj:addEventListener("tap", onTap)
I’m not sure I understand event.target. If I run the code below, it says that event.target is nil. Will someone help me?
obj = display.newImage("image.png") local function onTap() print(event.target) end obj:addEventListener("tap", onTap)
You must have defined a variable called ‘event’ somewhere earlier in the code because you don’t have an ‘event’ parameter on your ‘onTap’ function. Try this…
local obj = display.newImage("image.png") local function onTap( event ) print( event.target ) return true end obj:addEventListener( "tap", onTap )
That works. But why does it say “table: blahblahblah” in the terminal when I press the image? Does it mean it’s a table or a table value?
How would I compare event.target with a variable that I assigned a image to?
It says it is a table, but really it is a display object.
Look at this code, where I set a field on the object and then access it in onTap()
local w = display.contentWidth local h = display.contentHeight local centerX = w/2 local centerY = h/2 – == – string:rpad( len, char ) - Places padding on right side of a string, such that the new string is at least len characters long. – == function string:rpad(len, char) local theStr = self if char == nil then char = ’ ’ end return theStr … string.rep(char, len - #theStr) end – == – table.dump( theTable [, padding] ) - Dumps indexes and values inside single-level table (for debug) – == function table.dump(theTable, padding ) local padding = padding or 30 print("\Table Dump:") print("-----") if(theTable) then for k,v in pairs(theTable) do local key = tostring(k) local value = tostring(v) local keyType = type(k) local valueType = type(v) local keyString = key … " (" … keyType … “)” local valueString = value … " (" … valueType … “)” keyString = keyString:rpad(padding) valueString = valueString:rpad(padding) print( keyString … " == " … valueString ) end else print(“empty”) end print("-----\n") end local obj = display.newImage(“image.png”) obj.x = centerX obj.y = centerY obj.myName = “Bob” local function onTap( event ) print( event.target, event.target.myName ) table.dump( event.target ) return true end obj:addEventListener( “tap”, onTap )
You must have defined a variable called ‘event’ somewhere earlier in the code because you don’t have an ‘event’ parameter on your ‘onTap’ function. Try this…
local obj = display.newImage("image.png") local function onTap( event ) print( event.target ) return true end obj:addEventListener( "tap", onTap )
That works. But why does it say “table: blahblahblah” in the terminal when I press the image? Does it mean it’s a table or a table value?
How would I compare event.target with a variable that I assigned a image to?
It says it is a table, but really it is a display object.
Look at this code, where I set a field on the object and then access it in onTap()
local w = display.contentWidth local h = display.contentHeight local centerX = w/2 local centerY = h/2 – == – string:rpad( len, char ) - Places padding on right side of a string, such that the new string is at least len characters long. – == function string:rpad(len, char) local theStr = self if char == nil then char = ’ ’ end return theStr … string.rep(char, len - #theStr) end – == – table.dump( theTable [, padding] ) - Dumps indexes and values inside single-level table (for debug) – == function table.dump(theTable, padding ) local padding = padding or 30 print("\Table Dump:") print("-----") if(theTable) then for k,v in pairs(theTable) do local key = tostring(k) local value = tostring(v) local keyType = type(k) local valueType = type(v) local keyString = key … " (" … keyType … “)” local valueString = value … " (" … valueType … “)” keyString = keyString:rpad(padding) valueString = valueString:rpad(padding) print( keyString … " == " … valueString ) end else print(“empty”) end print("-----\n") end local obj = display.newImage(“image.png”) obj.x = centerX obj.y = centerY obj.myName = “Bob” local function onTap( event ) print( event.target, event.target.myName ) table.dump( event.target ) return true end obj:addEventListener( “tap”, onTap )