绘制多种类型的表格

时间:2017-09-15 12:53:18

标签: lua lua-table love2d

我有两个僵尸对象,一个是Grunt,另一个是Runner

我的Grunt.lua文件:

function InitGrunt()
  grunt = {}
  grunt.x = 0
  grunt.y = 0
  grunt.speed = 120
  grunt.hitBox = (sprites.grunt:getHeight() + sprites.grunt:getWidth())/2
  grunt.hit = false

  gruntDefCD = 2
  gruntCD = gruntDefCD
  gruntMinCD = 0.4
  gruntTimer = gruntCD
  gruntTimerDecr = 0.8
end

function SpawnGrunt()
  local side = math.random(1, 4)

  --randomize spawn position

  table.insert( zombies, grunt )
end

和我的Runner.lua

function InitRunner()
  runner = {}
  runner.x = 0
  runner.y = 0
  runner.speed = 240
  runner.hitBox = (sprites.runner:getWidth() + sprites.runner:getHeight())/2
  runner.hit = false

  runnerDefCD = 4
  runnerCD = runnerDefCD
  runnerMinCD = 2
  runnerTimer = runnerCD
  runnerTimerDecr = 0.95
end

function SpawnRunner()
      local side = math.random(1, 4)

      --randomize spawn position

      table.insert( zombies, runner )
end

所以zombie表格会有grunt个和runner个。如何在Draw()

中打印这两个

我可以画一个:

function DrawGrunts()
  for i, z in ipairs(zombies) do
    love.graphics.draw(sprites.grunt, z.x, z.y, PlayerZombieAngle(z), nil, nil, sprites.grunt:getWidth()/2, sprites.grunt:getHeight()/2 )
  end
end

但是如何在一个函数中绘制它们呢?理想情况下呢?

1 个答案:

答案 0 :(得分:4)

为不同的实体指定一个指向其精灵对象的指针:

runner = { }
runner.x = 0
runner.y = 0
runner.sprite = sprites.runner
-- Rest of runner definition

grunt = { } 
grunt.x = 0 
grunt.y = 0
grunt.sprite = sprites.grunt
-- Rest of grunt definition

你的绘图功能变为:

for i, z in ipairs(zombies) do
    love.graphics.draw(z.sprite, z.x, z.y, ...)
end