Reputation:
I am creating a OOP game in Corona SDK using metatables and am having some trouble with my code.
Here is my main.lua file:
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
-- Your code here
local hero = require("hero")
local environment = require("environment")
local obstacle = require("obstacle")
local player = hero.new("Billy", 0, 10)
Here is my hero.lua class file:
local hero = {}
local hero_mt = {_index = hero}
--Constructor
function hero.new (name, positionX, positionY)
local newHero = {
name = name
positionX = positionX or 0
positionY = positionY or 0
}
return setmetatable( newHero, herp_mt )
function hero:Jump(amount)
end
And the error I am receiving is as follows:
error loading module 'hero' from file 'hero.lua': hero.lua:14 '}' expected (to close '{' at line 12) near 'positionX'
I followed the same syntax this site used (https://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/) but still nothing is working. Any thoughts?
Upvotes: 2
Views: 303
Reputation: 1819
You are missing commas when declaring your newHero table. All tables must have their properties separated by commas. See the documentation for more information. The last element can have a comma as well.
local newHero = {
name = name,
positionX = positionX or 0,
positionY = positionY or 0,
}
You are missing a closing end
as well for the function hero.new()
And need to return the hero table at the end of your hero file, like so: return hero
so that you can actually call hero.new()
in your main file.
Upvotes: 2