1

Today I came across Corona SDK after using LÖVE for a while. I have stumbled upon a problem that I cannot seem to fix. The Bullets do spawn but they remain motionless. Is there another alternative to update every Bullet or am I doing something wrong?

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)

In LÖVE I could update every individual Bullet using:

function love.update(dt)
  for _,v in ipairs(Bullets) do v:update(dt) end
end
BruceTheGoose
  • 63
  • 1
  • 10
  • Loops are a construct of the language so they are the same regardless of the environment (Corona, Love). A clearer title and [MCVE](http://stackoverflow.com/help/mcve) might have better luck here. – ryanpattison Jan 12 '15 at 02:56

1 Answers1

1

Based on the loop you were using in Love, I would make the Bullets be an array of Bullet, and your update function should loop like you did in Love. So you need a few fixes:

1) everywhere in your posted code change Bullets to Bullet

2) replace these lines

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

with

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) make the new() add new Bullet to the Bullets list:

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

4) (probably, see note after) make the bullet remove itself from Bullets list when destroyed:

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

The last step, 4, may not be needed, based on your use of self.remove = true I'm guessing you have separate stages for "marking for deletion" and "actually delete" but you get the idea.

Oliver
  • 27,510
  • 9
  • 72
  • 103