Page 1 of 2
Anyone gonna help a newbie? Again... yet Again...
Posted: Fri Mar 25, 2011 5:10 pm
by LuaWeaver
Does anyone see an error in this code? LOVE doesn't say an error to me, but what it does is it doesn't work properly. This is my third topic on coding help
what this is supposed to do is build a grid using my images, fitting the size of the window perfectly. Without the color pieces added, it works fine. But, adding the changing one thing in the board table chanegs the entire row! It's laid out like this: A table containing other tables. When you press a button it checks to see if the the grid space is existent and empty. If so, it changes that value. In this code, it constantly draws that table, so if the table is changed, boom, it changes on the screen. I have the table change working, as said above. But, it changes the row, instead of one piece. If there is an error in this, then there's my problem. If not, I know where the problem is.
Code: Select all
function love.draw()
if not selected then
love.graphics.print("PLEASE SELECT A GRID SIZE 4-8", 200, 200)
else
for i=1, 8 do
row=board[i]
for l=1, 8 do
local piece=row[l]
if piece~=0 then
love.graphics.drawq(piece, grid, w*(i-1), he*(l-1), 0, 1, 1, 0, 0)
end
end
end
end
end
Re: Anyone gonna help a newbie? Again... yet Again...
Posted: Fri Mar 25, 2011 5:53 pm
by bartbes
You should really, really, really use descriptive topic names.
As for the problem itself, it looks like the offending code is the editing code, which you haven't posted.
Re: Anyone gonna help a newbie? Again... yet Again...
Posted: Fri Mar 25, 2011 7:43 pm
by LuaWeaver
Code: Select all
function love.keypressed(key)
if not selected then
for i=4, 8 do
if key==""..i then
selected=i
setGrid(i)
w=w/selected
he=he/selected
grid=love.graphics.newQuad(0, 0, w, he, w, he)
end
end
else
for i=1, 8 do
if ""..i==key then
changeBoard(i, 1)
end
end
end
end
function setGrid(size)
for i=1, size do
local h=board[i]
for i=1, size do
h[i]=empty
end
end
end
function changeBoard(key, row)
if board[row][key]~=0 then
if board[row][key]==empty then
if turn==red then
board[row][key]=red
turn=blue
else
board[row][key]=blue
turn=red
end
end
end
end
Re: Anyone gonna help a newbie? Again... yet Again...
Posted: Fri Mar 25, 2011 11:01 pm
by bartbes
LuaWeaver wrote:Code: Select all
function love.keypressed(key)
for i=1, 8 do
if ""..i==key then
changeBoard(i, 1)
end
end
Doesn't that code set the entire row?
Re: Anyone gonna help a newbie? Again... yet Again...
Posted: Sun Mar 27, 2011 2:36 pm
by LuaWeaver
How so? It goes to board[row][key] and changes it. How does it set the whole row?
Re: Anyone gonna help a newbie? Again... yet Again...
Posted: Sun Mar 27, 2011 2:50 pm
by Robin
LuaWeaver wrote:How so? It goes to board[row][key] and changes it. How does it set the whole row?
Because it is in a for-loop.
Re: Anyone gonna help a newbie? Again... yet Again...
Posted: Sun Mar 27, 2011 9:25 pm
by leiradel
Robin wrote:LuaWeaver wrote:How so? It goes to board[row][key] and changes it. How does it set the whole row?
Because it is in a for-loop.
But he only sets the element if ""..i==key...
A better way to write this is changeBoard(key + 0, 1). key + 0 will convert the string
key into a number and add zero to it.
As for the problem you're having, I don't know...
Re: Anyone gonna help a newbie? Again... yet Again...
Posted: Mon Mar 28, 2011 5:58 am
by Robin
leiradel wrote:But he only sets the element if ""..i==key...
A better way to write this is changeBoard(key + 0, 1). key + 0 will convert the string key into a number and add zero to it.
I'm wondering about the hacks. What's wrong with tostring() and tonumber()?
Re: Anyone gonna help a newbie? Again... yet Again...
Posted: Mon Mar 28, 2011 1:08 pm
by leiradel
Robin wrote:leiradel wrote:But he only sets the element if ""..i==key...
A better way to write this is changeBoard(key + 0, 1). key + 0 will convert the string key into a number and add zero to it.
I'm wondering about the hacks. What's wrong with tostring() and tonumber()?
Good point, it's just that old habits die hard.
Re: Anyone gonna help a newbie? Again... yet Again...
Posted: Mon Mar 28, 2011 8:58 pm
by LuaWeaver
leiradel wrote:Robin wrote:leiradel wrote:But he only sets the element if ""..i==key...
A better way to write this is changeBoard(key + 0, 1). key + 0 will convert the string key into a number and add zero to it.
I'm wondering about the hacks. What's wrong with tostring() and tonumber()?
Good point, it's just that old habits die hard.
If I know easy syntax for something, I don't investigate further. I actually prefer the "hacks" because I've used them longer AND I only know about those functions now...
anyways, I still can't find the error. Please start getting back on topic...