1

今天我在使用 LÖVE 一段时间后遇到了 Corona SDK。我偶然发现了一个我似乎无法解决的问题。子弹确实会生成,但它们仍然一动不动。是否有另一种方法可以更新每个 Bullet,或者我做错了什么?

Bullets = {}

Bullets.__index = Bullets

function Bullets.new()
  local this = {}
  this.x = Player.x
  this.remove = false
  this.dir = math.random(1,2)
  this.timer = 0
  this.velocity = math.random(2,5)
  if this.dir == 1 then
    this.y = display.actualContentHeight
  elseif this.dir == 2  then
    this.y = 0
  end
  this.rect = display.newRect(this.x,this.y,math.random(5,10),math.random(5,10))
  this.rect:setFillColor(0)
  return setmetatable(this,Bullets)
end

function Bullets.update(self,event)
   self:move()
end

function Bullets.move(self)
   if self.dir == 1 then
    self.y = self.y + self.velocity
  elseif self.dir == 2 then
    self.y = self.y - self.velocity
  end
end

function Bullets.destroy(self)
  self.remove = true
end

Bullets.enterFrame = Bullets.update

Runtime:addEventListener("enterFrame",Bullets)
timer.performWithDelay(500,Bullets.new,0)

在 LÖVE 中,我可以使用以下方法更新每个 Bullet:

function love.update(dt)
  for _,v in ipairs(Bullets) do v:update(dt) end
end
4

1 回答 1

1

根据您在 Love 中使用的循环,我会将Bullets设为 的数组Bullet,并且您的更新函数应该像在 Love 中一样循环。所以你需要一些修复:

1)您发布的代码中的任何地方都更改 BulletsBullet

2)替换这些行

Bullets.enterFrame = Bullets.update
Runtime:addEventListener("enterFrame",Bullets)
timer.performWithDelay(500,Bullets.new,0)

lastTime = 0
function updateBullets(event)
  local dt = lastTime - event.time
  lastTime = event.time
  for _,v in ipairs(Bullets) do v:update(dt) end
end
Runtime:addEventListener("enterFrame",updateBullets)
timer.performWithDelay(500,Bullet.new,0)

3)new()将新的 Bullet 添加到Bullets列表中:

function Bullet.new()
  local this = {}
  table.insert(Bullets, this)
  ... rest is same...
end

4)(可能,见后面的注释)使子弹在销毁时从子弹列表中删除:

function Bullet.destroy(self)
  self.remove = true
  remove = find_bullet_in_bullets_list(self)
  table.remove(Bullets, remove)      
end

最后一步,4,可能不需要,根据你的使用self.remove = true我猜你有“标记删除”和“实际删除”的单独阶段,但你明白了。

于 2015-01-12T05:43:15.167 回答