xNick1 wrote: ↑Fri Dec 15, 2017 6:11 pm
I said "or even".
You either call square.speed or call speed and declare the variable.
But I did so!
local square = {}
speed = 40
And changed this!
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + speed * dt
end
end
Step by step.
Then you're gonna investigate why your code didn't work.
It won't work anyway cause you're using an empty table now.
square.x doesn't exist anymore.
You needed it.
If you don't wanna use a table you have at least to declare a variable called x and use that one
Last edited by xNick1 on Fri Dec 15, 2017 6:35 pm, edited 1 time in total.
local square = {x = 10, y = 10, speed = 50}
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + speed * dt
end
end
function square.draw()
love.graphics.rectangle( 'fill', x, y, 50, 50 )
end
-- return the square local so we can access it outside of this file
return square
But now...
Error
square.lua:19: bad argument #2 to 'rectangle' (number expected, got nil)
Traceback
[C]: in function 'rectangle'
square.lua:19: in function 'draw'
main.lua:12: in function 'draw'
[C]: in function 'xpcall'
xNick1 wrote: ↑Fri Dec 15, 2017 6:36 pm
Now you're using the table again, so it's square.x in your draw callback etc
Please consider following a tutorial about variables and tables
I know about variables, but I'm doing this.
local square
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = 10 + 40 * dt
end
if love.keyboard.isDown("left") then
square.x = 10 - 40 * dt
end
if love.keyboard.isDown("up") then
square.y = 10 - 40 * dt
end
if love.keyboard.isDown("down") then
square.y = 10 + 40 * dt
end
end
function square.draw()
love.draw.rectangle('fill', 10, 10, 50, 50)
end
-- return the square local so we can access it outside of this file
return square
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + speed * dt
end
end
...
This one seems to be alright. Maybe the error now is somewhere else?
I thought it might help you to see a similar example to identify the problem you have in your code so I attached the moving square example.
Hope it helps.
To see the lua files, unpack it with any file archiver (7-zip, winrar, etc).
xNick1 wrote: ↑Fri Dec 15, 2017 6:36 pm
Now you're using the table again, so it's square.x in your draw callback etc
Please consider following a tutorial about variables and tables
I know about variables, but I'm doing this.
local square
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = 10 + 40 * dt
end
if love.keyboard.isDown("left") then
square.x = 10 - 40 * dt
end
if love.keyboard.isDown("up") then
square.y = 10 - 40 * dt
end
if love.keyboard.isDown("down") then
square.y = 10 + 40 * dt
end
end
function square.draw()
love.draw.rectangle('fill', 10, 10, 50, 50)
end
-- return the square local so we can access it outside of this file
return square
This way, square.x doesn't exist
And square wouldn't have any value, so you don't need it.
Please read the error messages, they're saying what's wrong with your code!
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + speed * dt
end
end
...
This one seems to be alright. Maybe the error now is somewhere else?
I thought it might help you to see a similar example to identify the problem you have in your code so I attached the moving square example.
Hope it helps.
To see the lua files, unpack it with any file archiver (7-zip, winrar, etc).
I did as you said, but...
Error: Syntax error: square.lua:11: unexpected symbol near '10'
stack traceback:
[C]: at 0x68d0f9a0
[C]: in function 'require'
main.lua:2: in main chunk
[C]: in function 'require'
[string "boot.lua"]:429: in function <[string "boot.lua"]:275>
[C]: in function 'xpcall'
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + speed * dt
end
end
...
This one seems to be alright. Maybe the error now is somewhere else?
I thought it might help you to see a similar example to identify the problem you have in your code so I attached the moving square example.
Hope it helps.
To see the lua files, unpack it with any file archiver (7-zip, winrar, etc).
He deleted the variables in the table. It was crashing cause there were no square.x etc
function square.update( dt )
if love.keyboard.isDown("right") then
square.x = square.x + speed * dt
end
if love.keyboard.isDown("left") then
square.x = square.x - speed * dt
end
if love.keyboard.isDown("up") then
square.y = square.y - speed * dt
end
if love.keyboard.isDown("down") then
square.y = square.y + speed * dt
end
end
...
This one seems to be alright. Maybe the error now is somewhere else?
I thought it might help you to see a similar example to identify the problem you have in your code so I attached the moving square example.
Hope it helps.
To see the lua files, unpack it with any file archiver (7-zip, winrar, etc).
He deleted the variables in the table. It was crashing cause there were no square.x etc