Multiple Classes in same file?[Solved]
Posted: Fri Nov 18, 2016 2:34 pm
Hello, I want to have one file with a class and classes whom inherits from the main class. Is that possible? When I return the main class, I can make the main class only, and if I return the various classes, only the main persists.
May I have to do multiple files to do the subclasses?
Here I have an example:
This make the Button class work
But if I do this, I can't make a SimpleButton who inherits Button.
Main in this last case:
Same if I do this:
Thanks for the help
May I have to do multiple files to do the subclasses?
Here I have an example:
This make the Button class work
Code: Select all
local class = require 'middleclass'
local Button = class 'Button'
SIMPLE = 0
TEXTURE = 1
function Button:initialize(dx,dy,dw,dh,background,txt,func)
self.x = dx or nil
self.y = dy or nil
self.height = dh or nil
self.width = dw or nil
self.background = background or love.graphics.setColor(255,255,255)
self.text = txt or nil
self.job = func or function() end
self.type = SIMPLE
end
function Button:setXY(dx,dy)
self.x = dx
self.y = dy
end
function Button:setX(dx)
self.x = dx
end
function Button:setY(dx,dy)
self.y = dy
end
--[[local SimpleButton = class('SimpleButton', Button)
function SimpleButton:initialize(dx,dy,dh,dw,background,txt,func)
self.x = dx or 0
self.y = dy or 0
self.height = dh or 0
self.width = dw or 0
self.background = background or love.graphics.setColor(255,255,255,0)
self.text = txt or nil
self.job = func or function() end
self.type = SIMPLE
end
local TextureButton = class('TextureButton', Button)
function TextureButton:initialize(dx,dy,dh,dw,background,func)
self.x = dx or 0
self.y = dy or 0
self.height = dh or 0
self.width = dw or 0
self.background = background or love.graphics.setColor(255,255,255,0)
self.job = func or function() end
self.type = TEXTURE
end
]]--
function Button:update(dt)
end
function Button:draw()
love.graphics.rectangle("fill",self.x,self.y,self.width,self.height)
love.graphics.print( self.text, self.x+self.width/2, self.y+self.height/2 )
end
return Button
Code: Select all
local class = require 'middleclass'
local Button = class 'Button'
SIMPLE = 0
TEXTURE = 1
function Button:initialize(dx,dy,dw,dh,background,txt,func)
self.x = dx or nil
self.y = dy or nil
self.height = dh or nil
self.width = dw or nil
self.background = background or love.graphics.setColor(255,255,255)
self.text = txt or nil
self.job = func or function() end
self.type = SIMPLE
end
function Button:setXY(dx,dy)
self.x = dx
self.y = dy
end
function Button:setX(dx)
self.x = dx
end
function Button:setY(dx,dy)
self.y = dy
end
local SimpleButton = class('SimpleButton', Button)
function SimpleButton:initialize(dx,dy,dh,dw,background,txt,func)
self.x = dx or 0
self.y = dy or 0
self.height = dh or 0
self.width = dw or 0
self.background = background or love.graphics.setColor(255,255,255,0)
self.text = txt or nil
self.job = func or function() end
self.type = SIMPLE
end
local TextureButton = class('TextureButton', Button)
function TextureButton:initialize(dx,dy,dh,dw,background,func)
self.x = dx or 0
self.y = dy or 0
self.height = dh or 0
self.width = dw or 0
self.background = background or love.graphics.setColor(255,255,255,0)
self.job = func or function() end
self.type = TEXTURE
end
function Button:update(dt)
end
function Button:draw()
love.graphics.rectangle("fill",self.x,self.y,self.width,self.height)
love.graphics.print( self.text, self.x+self.width/2, self.y+self.height/2 )
end
return Button
Code: Select all
local Button = require 'button'
function love.load()
aButton = SimpleButton:new(200,200,100,100,love.graphics.setColor(100,100,100),"hello",nil)
end
function love.update(dt)
end
function love.draw()
aButton:draw()
end
Same if I do this:
Code: Select all
return Button, SimpleButton, TexturedButton