Looks like you've got a bit of a fundamental misunderstanding regarding the "self" keyword and instances of your enemy class.
Lets run through this example:
Code: Select all
local myTable = { x = 0 }
function myTable:myFunction ( )
-- just increment table's x value
self.x = self.x + 1 -- self is inferred from the colon syntax
end
myTable:myFunction ( )
-- is functionally equivalent to
local myTable = { x = 0 }
function myTable.myFunction ( tab ) -- "tab" could be named "self" or whatever you want
tab.x = tab.x + 1
end
myTable.myFunction ( myTable )
myTable:myFunction () -- can mix and match too, just need to be careful!
As you see, the colon (":") is just syntactic sugar, that reduces some amount of typing. Because in lua you are decently commonly passing tables around, or calling functions contained in tables (that act on those tables' contents), this kind of shorthand can be helpful -- just at the cost of causing some confusion for beginner programmers.
As such, when you have code like...
That means that you are not passing an
enemy instance to the function, but rather the
enemy class itself. This is why in your code the "instance == self" check would never equate to true, because your class and its instances are different tables.
So, as you can see, you'll need to modify your "beginContact" function to instead act on your instances, rather than the enemy class itself. You may similarly want to do so on player class too, in case you ever want to spawn more than one copy of the player.
So...
Code: Select all
-- instead of this...
Enemy:takeDamage ( param1, param2, ... )
-- do this
Enemy.takeDamage (instanceOfEnemy, param1, param2, ... )
-- or this, assuming instances of your enemy class contain the takeDamage function
instanceOfEnemy:takeDamage ( param1, param2, ... )
I'll leave fixing up the code as an exercise for you to solve.