Parameters Changing Types

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
kylewatson98
Prole
Posts: 13
Joined: Thu Feb 28, 2013 9:02 pm

Parameters Changing Types

Post by kylewatson98 »

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:

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
And this the main lua file:

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
I tried passing strings through but they changed to tables :s please help.
Thanks in advance, Kyle.
LÖVE dem beats
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

Re: Parameters Changing Types

Post by LuaWeaver »

It does not switch to a boolean, you just never use it. Change:

Code: Select all

   txtbox.x = x
   txtbox.y = y
to

Code: Select all

   txtbox.x = x1
   txtbox.y = y1
because you use x1 and y1, not x and y. As for string->tables, I think you're just doing something very wrong.
"your actions cause me to infer your ego is the size of three houses" -finley
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Parameters Changing Types

Post by Boolsheet »

The issue that LuaWeaver spotted is actually an additional problem to something else.

You forgot the colon when you created your new function. When you call it, textbox gets assigned to the variable focus instead of self.

Code: Select all

function textbox.new(focus, xl, yl, width, height)
                ^
Shallow indentations.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 10 guests