Advertisement
zitot

Untitled

May 23rd, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.09 KB | None | 0 0
  1. ```function enemy.update(enemy)
  2.   enemy.x = enemy.x - enemy.spd
  3.   if enemy.x < 0 then
  4.     enemy.x = 900
  5.     enemy.y = math.random(25,575)
  6.     enemy.spd = math.random(1,10)
  7.   end
  8. end
  9. -- below is the same as enemy.update(enemy) seen above
  10. -- the : operator when defining a function automatically makes the first argument's name 'self'
  11. -- self's value is the first argument passed in
  12.  -- myenemy.update(myenemy) passes in myenemy as the first argument
  13.  -- myenemy:update() does the same thing
  14. function enemy:update()
  15.   self.x = self.x - self.spd
  16.   if self.x < 0 then
  17.     self.x = 900
  18.     self.y = math.random(25,575)
  19.     self.spd = math.random(1,10)
  20.   end
  21. end
  22. -- this is NOT the same as either of the two above
  23. -- the first argument is still called self, but you never use it
  24. -- instead, you used enemy
  25. -- the reason this is different, is because arguments are automatically local variables to the function
  26.  
  27. function enemy:update()
  28.   enemy.x = enemy.x - enemy.spd
  29.   if enemy.x < 0 then
  30.     enemy.x = 900
  31.     enemy.y = math.random(25,575)
  32.     enemy.spd = math.random(1,10)
  33.   end
  34. end
  35. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement