Page 1 of 1
Simple Coding Question...
Posted: Mon Dec 29, 2014 5:16 am
by michaeladair
So I have 3 previously defined variables by the user, r, g, and b. These are inputted by the user and should be numbers.
Here is where it is called.
Code: Select all
function load( ip, name, r, g, b)
-- Collider = HC( 100, on_collision )
cam = camera(vector(256, 208), 1)
love.graphics.setBackgroundColor( 50, 50, 50 )
love.graphics.setColor( r, g, b)
client = lube.client()
client:setHandshake("chillout")
client:setCallback(onReceive)
client:connect(ip, 18025) --82.24.82.156 marks
client:send( "name" .. name)
end
I'm not sure If I called it right...
Here is where the user inputs the variables... (Is there any easier way to add a form?)
Code: Select all
local name = ""
local ip = ""
local r = ""
local g = ""
local b = ""
local flag = 1
function love.draw()
love.graphics.print("ip: " .. ip, 10, 10)
love.graphics.print("name: " .. name, 10, 30 )
love.graphics.print("Red: " .. r, 10, 50)
love.graphics.print("Green: " .. g, 10, 70)
love.graphics.print("Blue: " .. b, 10, 90)
end
function love.keypressed( key )
if key == 'return' then
if flag == 1 then
flag = 2
elseif flag == 2 then
flag = 3
elseif flag == 3 then
flag = 4
elseif flag == 4 then
flag = 5
elseif flag == 5 then
playing( ip, name, r, g, b )
end
elseif key == 'backspace' then
if flag == 1 then
ip = string.sub(ip, 1, string.len( ip ) - 1)
elseif flag == 2 then
name = string.sub(name , 1, string.len( name ) - 1)
elseif flag == 3 then
r = string.sub(r , 1, string.len( r ) - 1)
elseif flag == 4 then
g = string.sub(g , 1, string.len( g ) - 1)
elseif flag == 5 then
b = string.sub(b , 1, string.len( b ) - 1)
end
else
if flag == 1 then
ip = ip .. key
elseif flag == 2 then
name = name .. key
elseif flag == 3 then
r = r .. key
elseif flag == 4 then
g = g .. key
elseif flag == 5 then
b = b .. key
end
end
end
Re: Simple Coding Question...
Posted: Mon Dec 29, 2014 6:29 am
by AlexCalv
I'm not sure of what you're asking really but you can simplify the drawing calls by doing this instead:
Code: Select all
love.graphics.print("IP: " ..ip.. " Name: " ..name.. " Red: " ..r.. " Green: " ..g.. " Blue: " ..b, 10, 10)
Or separate lines:
Code: Select all
love.graphics.print("IP: " ..ip.. "\nName: " ..name.. "\nRed: " ..r.. "\nGreen: " ..g.. "\nBlue: " ..b, 10, 10)
Re: Simple Coding Question...
Posted: Mon Dec 29, 2014 10:25 am
by zorg
I think i kind of understand what you want to do.
At a first glance, i only see one potential error;
Code: Select all
function load( ip, name, r, g, b) <--
Code: Select all
elseif flag == 5 then
playing( ip, name, r, g, b ) <--
end
elseif key == 'backspace' then
if those were ment to be the same thing, then rename one of them.
Re: Simple Coding Question...
Posted: Mon Dec 29, 2014 11:36 am
by michaeladair
The problem that I have is that it does not see the variables as numbers:
Code: Select all
ERROR
states/playing.lua: 11: Bad argument #1 to 'setColor' (number expected, got nil)
Line 11:
Playing.lua:
Code: Select all
players = {}
function onReceive(data)
players = TSerial.unpack( data )
end
function load( ip, name, r, g, b )
-- Collider = HC( 100, on_collision )
cam = camera(vector(256, 208), 1)
love.graphics.setBackgroundColor( 50, 50, 50 )
love.graphics.setColor( r, g, b)
client = lube.client()
client:setHandshake("chillout")
client:setCallback(onReceive)
client:connect(ip, 18025) --82.24.82.156 marks
client:send( "name" .. name)
end
function love.draw()
cam:attach()
for k, v in pairs(players) do
love.graphics.rectangle('fill', v.x, v.y, 32, 32)
love.graphics.print(v.name, v.x-10, v.y-20)
love.graphics.setColor(255, 255, 255) --White
end
cam:detach()
-- debug
drawMessages()
love.graphics.print( love.timer.getFPS(), 10, 10 )
end
function love.update( dt )
-- Collider:update( dt )
client:update(dt)
if love.keyboard.isDown('a') or love.keyboard.isDown('left') then
client:send('left')
elseif love.keyboard.isDown('d') or love.keyboard.isDown('right') then
client:send('right')
end
if love.keyboard.isDown('w') or love.keyboard.isDown('up') then
client:send('up')
elseif love.keyboard.isDown('s') or love.keyboard.isDown('down') then
client:send('down')
end
end
function love.keypressed( key )
end
function love.keyreleased( key )
end
function love.mousepressed( x, y, key )
end
function love.quit()
client:send('quit')
end
Menu.lua:
Code: Select all
local name = ""
local ip = ""
local r = ""
local g = ""
local b = ""
local flag = 1
function love.draw()
love.graphics.print("ip: " .. ip, 10, 10)
love.graphics.print("name: " .. name, 10, 30 )
love.graphics.print("Red: " .. r, 10, 50)
love.graphics.print("Green: " .. g, 10, 70)
love.graphics.print("Blue: " .. b, 10, 90)
end
function love.keypressed( key )
if key == 'return' then
if flag == 1 then
flag = 2
elseif flag == 2 then
flag = 3
elseif flag == 3 then
flag = 4
elseif flag == 4 then
flag = 5
elseif flag == 5 then
playing( ip, name, r, g, b )
end
elseif key == 'backspace' then
if flag == 1 then
ip = string.sub(ip, 1, string.len( ip ) - 1)
elseif flag == 2 then
name = string.sub(name , 1, string.len( name ) - 1)
elseif flag == 3 then
r = string.sub(r , 1, string.len( r ) - 1)
elseif flag == 4 then
g = string.sub(g , 1, string.len( g ) - 1)
elseif flag == 5 then
b = string.sub(b , 1, string.len( b ) - 1)
end
else
if flag == 1 then
ip = ip .. key
elseif flag == 2 then
name = name .. key
elseif flag == 3 then
r = r .. key
elseif flag == 4 then
g = g .. key
elseif flag == 5 then
b = b .. key
end
end
end
Re: Simple Coding Question...
Posted: Mon Dec 29, 2014 12:30 pm
by zorg
The problem is that you're not passing those values to your load function, hence they are nil, as in, they don't exist.
Again, from what code you posted, i can not see where you call that function even.
Re: Simple Coding Question...
Posted: Mon Dec 29, 2014 1:21 pm
by michaeladair
zorg wrote:The problem is that you're not passing those values to your load function, hence they are nil, as in, they don't exist.
Again, from what code you posted, i can not see where you call that function even.
How do I pass the values? The ip and name are passed??
Re: Simple Coding Question...
Posted: Mon Dec 29, 2014 1:53 pm
by zorg
If those are, then as i said, from the code snippets you posted, i could not figure out the problem; sorry.
Either someone else will figure it out, or just post a .love file, or a zip with all the files (including your main.lua) or something, because i feel that something's still missing from the big picture.
Re: Simple Coding Question...
Posted: Mon Dec 29, 2014 2:00 pm
by michaeladair
zorg wrote:If those are, then as i said, from the code snippets you posted, i could not figure out the problem; sorry.
Either someone else will figure it out, or just post a .love file, or a zip with all the files (including your main.lua) or something, because i feel that something's still missing from the big picture.
Oh, i forgot to add the variables to the main.lua... it worked but now I have a problem with the rectangle not being written in the correct color.
It ends up writing the rectangle in white still.
Re: Simple Coding Question...
Posted: Wed Dec 31, 2014 4:04 am
by Positive07
You have this code in love.draw
Code: Select all
for k, v in pairs(players) do
love.graphics.rectangle('fill', v.x, v.y, 32, 32)
love.graphics.print(v.name, v.x-10, v.y-20)
love.graphics.setColor(255, 255, 255) --White
end
The first time you draw the rectangle you get the right color, but the second time, you have set the color to white... so everything you draw after the first iteration is white, if you want to change this
Then in load change this
to this
And in your for loop do this
Code: Select all
for k, v in pairs(players) do
love.graphics.setColor(color)
love.graphics.rectangle('fill', v.x, v.y, 32, 32)
love.graphics.setColor(255, 255, 255) --White
love.graphics.print(v.name, v.x-10, v.y-20)
end
I put white before the print, I guess that is what you really wanted, otherwise the print would have the same color as the rectangle, if that is not what you wanted then do this instead
Code: Select all
for k, v in pairs(players) do
love.graphics.setColor(color)
love.graphics.rectangle('fill', v.x, v.y, 32, 32)
love.graphics.print(v.name, v.x-10, v.y-20)
end
love.graphics.setColor(255, 255, 255) --White