Lua corona sdk/solar2d多个移动对象

Lua corona sdk/solar2d多个移动对象,lua,coronasdk,solar2d,Lua,Coronasdk,Solar2d,我无法以随机的移动速度创建多个对象。 例如,我需要制作1000个对象,并以随机速度在随机方向上移动它们 OOP aproach不起作用,但在love2d中,它可以毫无问题地工作 local displayWidth = display.contentWidth local displayHeight = display.contentHeight particle = {} particle.__index = particle ActiveParticle = {} function p

我无法以随机的移动速度创建多个对象。 例如,我需要制作1000个对象,并以随机速度在随机方向上移动它们

OOP aproach不起作用,但在love2d中,它可以毫无问题地工作

local displayWidth = display.contentWidth
local displayHeight = display.contentHeight

particle = {}
particle.__index = particle

ActiveParticle = {}

function particle.new()
  instance = setmetatable({}, particle)
  instance.x = math.random(20, displayWidth)
  instance.y = math.random(20, displayHeight)
  instance.xVel = math.random(-150, 150)
  instance.yVel = math.random(-150, 150)
  instance.width = 8
  instance.height = 8
  table.insert(ActiveParticle, instance)
end


function particle:draw()
  display.newRect(self.x, self.y, self.width, self.height)
end

function particle.drawAll()
  for i,instance in ipairs(ActiveParticle) do
    particle:draw()
  end
end

function particle:move()
  self.x = self.x + self.xVel
  self.y = self.y + self.yVel
end

for i = 1, 10 do
  particle.new()
  particle.drawAll()
end

function onUpdate (event)
  instance:move()
end

Runtime:addEventListener("enterFrame", onUpdate)
此代码不起作用,似乎solar2d不识别“self”

function particle.new()
  instance = setmetatable({}, particle)
  instance.x = math.random(20, displayWidth)
  instance.y = math.random(20, displayHeight)
  instance.xVel = math.random(-150, 150)
  instance.yVel = math.random(-150, 150)
  instance.width = 8
  instance.height = 8
  table.insert(ActiveParticle, instance)
end
实例
应该是本地的

应该使用
instance:draw()
来绘制实例,而不是
particle
。否则,
self
将不会引用
实例
,因此您无法访问其成员

或者使用
particle.draw(实例)


由于
\u索引
元方法
实例:draw()
将解析为
粒子。draw(实例)
因此在
粒子内部。draw
self
指的是
实例

你对它不起作用是什么意思?你面临错误吗?或者,预期行为与实际行为有何不同?是的,我有编译错误。本地实例也不起作用。在corona/solora2d中,可能有不同的解决方案来创建具有自参数的对象?您不认为与我们共享该错误消息有意义吗?main.lua:22错误参数#1到“newRect”(预期数量,为零)堆栈回溯:[C]:在函数“newRect”main中。lua:22在函数“draw”main中。lua:27:在函数“drawAll”main中。lua:38:在主块中查看我编辑的答案。
function particle.drawAll()
  for i,instance in ipairs(ActiveParticle) do
    particle:draw()
  end
end