When I use the function “haha()” it does not work, but just calling the variable “str” returns the proper value.
Code: (just defined one of these in main and then tried to use the “haha()” function)
TouchHandler = class()
function TouchHandler:init()
self.touches = {}
self.touchstate = {}
self.str = "stupid computer......"
end
function TouchHandler:touched(touch)
self.touches[touch.id] = touch
self.touchstate[touch.id] = touch.state
end
function TouchHandler:count()
local t = 0
for k,touch in pairs(self.touches) do
if self.touchstate[touch.id] == BEGAN or self.touchstate[touch.id] == MOVING then
t = t + 1
end
end
return t
end
function TouchHandler:haha()
return self.str
end
It’s a common and easy mistake to make. It doesn’t help that Lua’s typing isn’t very strong, so you can do stuff by accident that requires specific declarations in other languages.
In c#, for example, you’d never get away with calling TouchHandler.haha(), unless haha() was declared as a static method. And if you did declare haha() as static, it wouldn’t compile because .str isn’t static. And if you declared .str as static… well, everything would work, and .str would be shared across all instances of your class.
That’s one of the things I don’t like about scripting languages: when you get away from strong typing, you entirely lose the compiler’s ability to smack you upside the head and tell you’re being an idiot.
And trust me, I need that particular bit of assistance. I make a lot of dumb mistakes when coding, and the compiler is very busy telling me “hey stupid. You can’t do that!”