Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

love2d - Unable to declare a new function in lua, compiler tells me to include an '=' sign near the name of the function

So i have this Util.lua file in which i am making all the functions which will be used across all the states of my game.

This is what my Util File is like

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

note that the last function didn't work at all so i just returned counter for testing, the error message given is :

'=' expected near 'GenerateQuadsPowerups'

"Powerup" is a class i declared using a library class.lua. When i removed the problematic function from Util.lua, the same error was given on the first function i made in the Powerup.lua file.

Here's the class in case it is needed for reference

Powerup = Class{}

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

-- we only need to check collision with the paddle as only that collisionis relevant to this     class
function Powerup:collides()
  if self.x > paddle.x or paddle.x > self.x then
    return false
  end

  if self.y > paddle.y or 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

I can't understand what's going on here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Typo 1

funtion GenerateQuadsPowerups()

Typo 2

funtion Powerup:init()

Typo 3

funtion Powerup:update(dt)

self = nil achieves nothing btw. I guess you thought you could somehow destroy your PowerUp that way but it will only assign nil to self which is just a variable local to Powerup:update. It will go out of scope anyway.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...