So I got this chunk of code that for some reason is giving me a null exception on LOVE
function enemies_controller:spawnEnemy(x, y, rad)
enemy = {}
enemy.x = x
enemy.y = y
enemy.rad = rad
enemy.speedMulti = 1
table.insert(enemies_controller.enemies, enemy)
end
And the function calling it like this:
enemies_controller.spawnEnemy(100, 100, 50)
The thing is that is giving me a null exception on the enemy.rad when i draw, cause enemy.x takes the second function parameter and enemy.y the third, and none takes the first one... So i am unsure of what is happening
It's because using :
creates a "self" parameter. (This is a typing shortcut because lots of people make functions with self parameters. It doesn't do anything special.)
function enemies_controller:spawnEnemy(x, y, rad)
is the same as
function enemies_controller.spawnEnemy(self, x, y, rad)
or in other words:
enemies_controller.spawnEnemy = function(self, x, y, rad)
Notice the difference between .
and :
.
A similar thing when you call it:
enemies_controller:spawnEnemy(100, 100, 50)
is the same as:
enemies_controller.spawnEnemy(enemies_controller, 100, 100, 50)
and the first one is probably how you're meant to call the function - there's no other reason they'd define it with :
.
If you call
enemies_controller.spawnEnemy(100, 100, 50)
then you pass 100 as self
, 100 as x
and 50 as y
and nil as rad
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.