无法在lua中声明新函数,编译器告诉我在函数名称附近包含一个'='符号

所以我有这个Util.lua文件,在其中制作将在游戏的所有状态中使用的所有函数。

这是我的Util文件

function GenerateQuads(atlas, tilewidth, tileheight)
local sheetWidth = atlas:getWidth() / tilewidth
local sheetHeight = atlas:getHeight() / tileheight

local sheetCounter = 1
local spritesheet = {}

for y = 0, sheetHeight - 1 do
    for x = 0, sheetWidth - 1 do
        spritesheet[sheetCounter] =
            love.graphics.newQuad(x * tilewidth, y * tileheight, tilewidth,
            tileheight, atlas:getDimensions())
        sheetCounter = sheetCounter + 1
    end
end

return spritesheet
end

function table.slice(tbl, first, last, step)
local sliced = {}

for i = first or 1, last or #tbl, step or 1 do
  sliced[#sliced+1] = tbl[i]
end

return sliced
end

funtion GenerateQuadsPowerups()
  local counter = 1
  local quads = {}
  return counter
end

请注意,最后一个函数根本没有起作用,所以我只是为了测试返回了计数器,给出的错误信息是:

在'GenerateQuadsPowerups'附近需要'='符号

“Powerup”是我使用库class.lua声明的类。当我从Util.lua中删除有问题的函数时,相同的错误会在我在Powerup.lua文件中制作的第一个函数上给出。

如果需要参考,这是类的情况

Powerup = Class{}

funtion Powerup:init()
  self.x = VIRTUAL_WIDTH
  self.y = VIRTUAL_HEIGHT
  self.dx = 0
  self.dy = -10
end

--我们只需要检查与挡板的碰撞,因为只有该碰撞与该类有关
function Powerup:collides()
  if self.x > paddle.x 或 paddle.x > self.x then
    return false
  end

  if self.y > paddle.y 或 self.y > paddle.y then
    return false
  end

  return true
end

funtion Powerup:update(dt)
  self.y = self.y + self.dy * dt

  if self.y <= 0 then
    gSounds ['wall-hit']:play()
    self = nil
  end
end

我不明白这里发生了什么

点赞
用户2858170
用户2858170

Typo 1

函数 GenerateQuadsPowerups()

Typo 2

函数 Powerup:init()

Typo 3

函数 Powerup:update(dt)

self = nil 实际上没有任何作用。我猜您可能认为可以通过这种方式销毁 PowerUp,但这只会将 nil 赋给 self,它只是 Powerup:update 的本地变量。它将无论如何退出作用域。

2021-01-06 07:29:44