Lua 在使用多个对象时遇到问题

Lua 在使用多个对象时遇到问题,lua,Lua,关于OOP的问题 Invader = {PosX = 5, PosY = 5, alive = true} function Invader:new(x, y) -- local InvaderImage = paintUtils.loadImage("") self.__index = self self.PosX = x term.setCursorPos(self.PosX, self.PosY) write("V"

关于OOP的问题

Invader = {PosX = 5, PosY = 5, alive = true}    


function Invader:new(x, y)



 --  local InvaderImage = paintUtils.loadImage("")


    
self.__index = self
self.PosX = x
term.setCursorPos(self.PosX, self.PosY)
write("V")   
        
    
function refreshInvader()
    
    write("moved")
    term.setCursorPos(self.PosX, self.PosY)
    write(" ")
    self.PosX = self.PosX + 3
    term.setCursorPos(self.PosX, self.PosY)
    write("V")

end



end

如果我调用RefreshInjector,只有我创建的最新版本才会移动。还有什么可以移动所有的吗?

您只有一个
入侵者表。您的所有操作都指向
self
,它是
入侵者

在代码中任何时候都不会创建第二个表作为“新对象”

要获得多个对象,必须执行以下操作:

Pet = {}
function Pet:new(name, sound)

  self.__index = self

  local newObject = setmetatable({}, self)
  newObject.name = name or "unnamed"

 return newObject

end

local a = Pet:new("Snuggles")
local b = Pet:new("Nibbles")