As some of you may have seen in my previous thread I had taken some of the code from the game Kunosuke so as to understand its menu system and the OOP capabilities of middleclass. Now I have run into another brick wall.
When I run my simple little program I can't get the highlighting to work. When the user pressed up or down on the arrow keys it should change the highlighting of the words. Yet it doesn't change anything. Le code:
main.lua
Code: Select all
require "menus.lua"
function love.load()
end
function love.update(dt)
game = Game:new()
end
function love.draw()
game:drawMenus()
end
function love.keypressed(key, unicode)
game:keypressed(key, unicode)
end
Code: Select all
require "middleclass.lua"
-- require "Stateful.lua"
Game = class('Game')
--[[ 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()
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, "Credits")
table.insert(self.menu, "Quit")
end
end
-- Kindly lifted from Kurosuke.
function Game:keypressed(key, unicode)
if key == "up" then
if self.index > 1 then
print("Index is greater than 1.")
self.index = self.index - 1
print("Going "..key.."!")
print(" The value of index is "..self.index)
if self.menu[self.index] == " " then -- Does this check to see if the table is empty?
self.index = self.index - 1
end
else
print("Now equating index to the number of menu elements.")
self.index = #self.menu
end
elseif key == "down" then
if self.index < #self.menu then
self.index = self.index + 1
print("Going "..key.."!")
print(" The value of index is "..self.index)
if self.menu[self.index] == " " then
self.index = self.index + 1
end
else
self.index = 1
print("The value of self.index is "..self.index)
end
elseif key == "return" then
Game:makeSenseOfItAll()
end
end
-- Lovingly lifted from the game Kurosuke.
function Game:drawMenus()
love.graphics.setColor(255, 255, 255, 255)
-- 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.
self.index = 1
end
-- What happens when the player presses return on the menu
-- select screen.
function Game:makeSenseOfItAll()
local menuSelection = self.menu[self.index]
if menuSelection == "Start Game" then
Game:gotoState("Main game") -- This is for starting the actual game.
elseif menuSelection == "Quit" then
love.event.push('q')
end
end
I've been racking my brain on this one, going through the code several times. I can't find any flaws with it.