I have been trying to use middleclass, but when I do the variable "self" isn't found when I use it. When I run the program LÖVE keeps giving me an error about indexing a local variable, even though I clearly required "middleclass". I even just requiring "middleclass.lua" and that didn't work.
I took some of the code from the game Kurosuke and messed around with that. But that stuff is not even running:
Code: Select all
require "middleclass"
require "Stateful.lua"
Game = class('Game', StatefulObject)
--[[ Taken from the game Kurosuke (or at least based upon code taken
from the game Kurosuke). ]]
function Game:initialize()
super.initialize(self)
self.menu = {}
self.index = 1
self.topMenu = "top"
self.showMenus()
self.drawMenus()
end
function Game:showMenus()
-- Wipe her clean.
for i=1, #self.menu do
table.remove(self.menu, 1)
end
if self.topMenu == "top" then
table.insert(self.menu, "Start Game")
table.insert(self.menu, "Quit")
end
end
-- Would this even work?
-- Kindly lifted from Kurosuke.
function Game:keypressed(key, unicode)
if key == "up" then
if self.index > 1 then
self.index = self.index - 1
if self.menu[self.index] == " " then -- Does this check to see if the table is empty?
self.index = self.index - 1
end
end
elseif key == "down" then
if self.index < #self.menu then
self.index = self.index + 1
if self.menu[self.index] == " " then
self.index = self.index + 1
end
end
elseif key == "return" then
Game:makeSenseOfItAll()
end
end
-- Lovingly lifted from the game Kurosuke.
function Game:drawMenus()
-- Set some colors and banners here.
for i=1,#self.menu do
if i == self.index then
-- Set color for highlighting.
love.graphics.setColor(255,240,25,100)
else
-- Set normal color.
love.graphics.setColor(11,47,255,100)
end
love.graphics.print(self.menu[i], love.graphics.getWidth() / 2, (love.graphics.getHeight() / 2) + 32 * i)
end
-- Possibly draw the cursor here.
end
-- What happens when the player presses return on the menu
-- select screen.
function Game:makeSenseOfItAll()
local menuSelection = self.menu[self.index]
if menuSelection == "top" then
Game:gotoState("Main game") -- This is for starting the actual game.
elseif menuSelection == "Quit" then
love.event.push('q')
end
end