Now on to the menus...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Now on to the menus...

Post by Ertain »

Hello again, LÖVE'rs.

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
menus.lua

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
At runtime, when you press up, it should add 1 to the Game:index. Apparently, it doesn't add anything.

I've been racking my brain on this one, going through the code several times. I can't find any flaws with it.
Booted, suited, and ready to get executed.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Now on to the menus...

Post by tentus »

The if self.menu[self.index] == " " then code lets me put blank lines in the menu. It's a hack, but it works ok.

I didn't see any reference to the menupointer, which does the highlighting, so to speak. In Kurosuke, at least ATM, menuPointer:draw() gets called near the of the draw function, operating in apparent (but not actual) synchonization with the menu. In reality, the text and menuPointer are divorced in function, so that I can reuse the menuPointer for other things.

This makes me realize I need to completely rehaul the menuPointer. Dang.
Kurosuke needs beta testers
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Re: Now on to the menus...

Post by Ertain »

Wow, didn't think I'd actually get a response from the guy who wrote it.

So the menuPointer code actually does the coloring? Then I shall look into that.
*Looks into that*
Ah crap, I still can't get the darn thing to work. Even when I wrote code for a simple pointer it doesn't move. Here's the pointer code, if you're wondering:

Code: Select all

require "middleclass.lua"

Pointer = class('Pointer')

function Pointer:initialize()
  super.initialize(self)
  self.theActualPointer = "-->"
end

function Pointer:draw(theIndex)
  love.graphics.setColor(255, 255, 255, 255)
  love.graphics.print(self.theActualPointer, love.graphics.getWidth()/2-32, (love.graphics.getHeight() / 2) + (32 * theIndex))
end
The variable, "theIndex", is the current value of self.index.
Booted, suited, and ready to get executed.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests