creating a menu with middleclass and love

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
Lethys
Prole
Posts: 1
Joined: Sat Jan 29, 2011 11:04 pm

creating a menu with middleclass and love

Post by Lethys »

Hi, i'm a new user of löve, and i'm new in programming some regular code. I said regular because i know a bit oh php and all stuff for the web... but it's very different i think with something like lua ^^.

So for the moment i just want to remember my old knowledge in java when i was at school, just for remember how to code...

And i just want to learn löve and lua a bit before thinking about making games.

So for the moment i'm just trying to make a menu.


At this moment i have a class named Button, the object have a x position, y position, a text, an image when it's unpressed and an image when it's pressed.
For the moment, when i clic on the button, the image that is displayed is changed to be the pressed image... but when i released my left mouse button, i want it to change to be the unpressed image...
My function love.mouse.released is

Code: Select all

function love.mousereleased(x, y, button)
   if button == 'l' then
      self.imageD = self.imageU
   end
end
But i know the self.imageD cannot be here... but i don't know how to do this...
I'm just asking myself if there is a way to call a function from my button class, and if it's can be done, well i'll do a Button:unclic() and put into the love.mousereleased function..
this is my entire class button :

Code: Select all

require "class/kikito-middleclass-39aebfc/middleclass.lua"

Button = class('Button')

function Button:initialize(x,y,text,imageU,imageP,imageD)
  self.x = x
  self.y = y
  self.text = text
  self.imageU = imageU
  self.imageP = imageP
  self.imageD = imageU
end

function Button:setX(x)
    self.x = x
end

function Button:getX(x)
	return self.x
end

function Button:setY(y)
    self.y = y
end

function Button:getY(y)
	return self.y
end

function Button:setText(text)
    self.text = text
end

function Button:getText(text)
	return self.text
end

function Button:setImageU(imageU)
    self.imageU = imageU
end

function Button:getImageU(imageU)
    return self.imageU
end

function Button:setImageP(imageP)
    self.imageP = imageP
end

function Button:getImageP(imageP)
    return self.imageP
end

function Button:isClic()
	width = self.imageU:getWidth()
	bottom_right_x = self.x + width
	
	height = self.imageU:getHeight()
	bottom_right_y = self.y + height

	mouse_x = love.mouse.getX()
	mouse_y = love.mouse.getY()
	
	if love.mouse.isDown("l") and mouse_x > self.x and mouse_x < bottom_right_x and mouse_y > self.y and mouse_y < bottom_right_y then 
	self.imageD = self.imageP
	end
end

function love.mousereleased(x, y, button)
   if button == 'l' then
      self.imageD = self.imageU
   end
end

function Button:display()
	love.graphics.draw(self.imageD,self.x,self.y)
	width = self.imageU:getWidth()
	text_x = self.x + width * 0.4
	height = self.imageU:getHeight()
	text_y = self.y + height* 0.2
	love.graphics.print(self.text, text_x, text_y)
end
and now this is my main.lua

Code: Select all

require "class/classes.lua"

unpressButton = love.graphics.newImage("imageButtonU.png")
pressButton = love.graphics.newImage("imageButtonP.png")

menu_play = Button(100,200,"Play",unpressButton,pressButton)

function love.load()
	love.graphics.setMode( 1000, 500, false )
	local f = love.graphics.newFont(love._vera_ttf, 14)
    love.graphics.setFont(f)
end

function love.draw()
	menu_play:display()
end

function love.update(dt)
	menu_play:isClic()
	quit()
end

function quit()
	if love.keyboard.isDown("escape") then
	love.event.push('q')
	end
end

If there is someone to help me that would be great.

ps : excuse my english, i'm french...
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: creating a menu with middleclass and love

Post by Robin »

It's a bit difficult to follow, but I think what you want is something like this:

Code: Select all

function love.mousereleased(x, y, button)
   if button == 'l' then
      self.image = self.imageU
   end
end
And draw self.image, rather than one of self.imageU or self.imageD or self.imageP.

You say you're French, perhaps there is a fellow Frenchman in here who would like to discuss this privately with you in your native language. :)
Help us help you: attach a .love.
Post Reply

Who is online

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