Parameters Changing Types
Posted: Tue Apr 23, 2013 1:11 pm
Hey, I am trying to make a global textbox class to use in my games but I have ran into a problem :/ When I pass numbers through my parameters of a function they switch to booleans :/
This is the class:
And this the main lua file:
I tried passing strings through but they changed to tables :s please help.
Thanks in advance, Kyle.
This is the class:
Code: Select all
textbox = {}
textbox.__index = textbox
function textbox.new(focus, xl, yl, width, height)
local txtbox = {}
setmetatable(txtbox,textbox)
txtbox.text = "Enter Text"
txtbox.hasFocus = focus
txtbox.x = x
txtbox.y = y
txtbox.width = width
txtbox.height = height
return txtbox
end
function textbox:draw()
love.graphics.setColor(255,255,255,255)
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
love.graphics.setColor(0,0,0,255)
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
love.graphics.printf(self.text, self.x + 5, self.y, self.width - 5, "left")
end
function textbox:keypress(k)
if self.hasFocus then
if k == "backspace" then
self.text = string.sub(self.text, 1, string.len(self.text) - 1)
elseif k == "return" then
self.text = self.text .. "\n"
else
self.text = self.text .. k
end
end
end
function textbox:mousepress(b)
if b == "l" then
if love.mouse.getX() > self.x and self.x + self.width > love.mouse.getX() and love.mouse.getY() > self.y and self.y + self.width > love.mouse.getY() then
self.hasFocus = true
else
self.hasFocus = false
end
end
end
Code: Select all
function love.load()
love.graphics.setBackgroundColor(210,255,255,255)
require "globalTextbox"
txt = textbox:new(false, 10, 10, 200, 50)
end
function love.draw()
txt:draw()
end
function love.keypressed(k)
txt:keypress(k)
end
function love.mousepressed(b)
txt:mousepress(b)
end
Thanks in advance, Kyle.