Saintgio

[Lua] [es] Ejemplo de objeto

May 21st, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.20 KB | None | 0 0
  1. Balon = {x = 0, y = 0, vx = 0, vy = 0}
  2.  
  3. function Balon:new(o, x, y)
  4.     o = o or {}
  5.     setmetatable(o, self)
  6.     self.__index = self
  7.     self.x = x or 400
  8.     self.y = y or 200
  9.     return o
  10. end
  11.  
  12. function Balon:print_pos()
  13.     print(string.format("x: %s, y: %s",self.x, self.y))
  14. end
  15.  
  16. function Balon:obtener_pos()
  17.     return self.x, self.y
  18. end
  19.  
  20. function Balon:definir_pos(x, y)
  21.     ultima_actualizacion = os.time()
  22.     self.x = x
  23.     self.y = y
  24. end
  25.  
  26. function Balon:dibujar()
  27.     local x, y = self.x, self.y
  28.     particula(1, x, y-5)
  29.     particula(1, x-5, y)
  30.     particula(1, x+5, y)
  31.     particula(1, x, y+5)
  32. end
  33.  
  34. particula = tfm.exec.displayParticle
  35. ultima_actualizacion = os.time()
  36. local balon = Balon:new(nil)
  37. local nombre = "Saintgio"
  38.  
  39. function eventLoop(elapsedTime, remainingTime)
  40.     if os.time()-ultima_actualizacion > 400 then
  41.         balon:definir_pos(tfm.get.room.playerList[nombre].x, tfm.get.room.playerList[nombre].y)
  42.     end
  43.     balon:dibujar()
  44. end
  45.  
  46. function eventKeyboard(playerName, keyCode, down, xPlayerPosition, yPlayerPosition)
  47.     balon:definir_pos(xPlayerPosition, yPlayerPosition)
  48. end
  49.  
  50.  
  51. for _, keyCode in pairs({0,1,2,3}) do
  52.     system.bindKeyboard(nombre, keyCode, true)
  53.     system.bindKeyboard(nombre, keyCode, false)
  54. end
Advertisement
Add Comment
Please, Sign In to add comment