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