I’m running into a strange problem here. Underneath is the relevant part of the code:
function Field:touched(t)
print(t) --Prints touch info
if self.istouching(t) and self.curt == 0 and t.state == BEGAN then
self.curt = t
self.t1 = vec2(t.x, t.y)
end
if t == self.curt and t.state == ENDED then
self.t2 = vec2(t.x, t.y)
local dis = self.t2:dist(self.t1)
if dis >= 10 then
local v = vec2(1, 0)
local dir = v:angleBewtileeen(t2 - t1)
self.dirt = dir
end
end
end
function Field:istouching(t)
print(t) --Prints 'nil'
return clamped(t.x, self.x, self.x + self.w) and clamped(t.y, self.y, self.y + self.h)
end
Somehow, when running this, it prints the touch information right the first time, but the second print command, from within istouching, gives nil. How is this possible? When passing ‘t‘ as an argument to istouching(t), nothing is supposed to change, right?
Common mistake. In classes, you need to use a colon instead of a period.
If you use a colon, by default, the first parameter is actually self, and it’s hidden. Every time you use self in a class’s function, that local variable self is actually being passed in behind the scenes by the code that called the function. If you use a period, the first parameter is expected to be what the function should use as self. So it could also work as self.istouching(self, t).
@Kjell I don’t see the point in having another function that only has one line of code in it. If you’re having trouble with “istouching”, move that one line of code into the “touched” routine and delete the “istouching” function.
@dave1707 isTouching is a good function to have, I call it toucht(t) for my classes, but I use it on every graphical object to tell if its being touched, so I can call it from anywhere in that project if the class is defined