Page 2 of 4
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:14 pm
by SynfigMaster91
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
But nothing!
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:32 pm
by xNick1
square.x = square.x + 40 * dt
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
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:34 pm
by SynfigMaster91
xNick1 wrote: ↑Fri Dec 15, 2017 6:11 pm
I said "or even".
You either use square.speed or use speed and declare the variable.
Please consider using some Lua tutorial:
http://tylerneylon.com/a/learn-lua/
You won't have such issues anymore!
Nah man, love is user friendly.
It's just picky about who his friends are
I did this:
Code: Select all
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'
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:36 pm
by xNick1
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
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:44 pm
by SynfigMaster91
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
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:47 pm
by MadByte
SynfigMaster91 wrote: ↑Fri Dec 15, 2017 6:14 pm
...
local square = {}
speed = 40
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).
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:51 pm
by xNick1
SynfigMaster91 wrote: ↑Fri Dec 15, 2017 6:44 pm
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!
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:52 pm
by SynfigMaster91
MadByte wrote: ↑Fri Dec 15, 2017 6:47 pm
SynfigMaster91 wrote: ↑Fri Dec 15, 2017 6:14 pm
...
local square = {}
speed = 40
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'
I'm more desperate than ever!
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:54 pm
by xNick1
MadByte wrote: ↑Fri Dec 15, 2017 6:47 pm
SynfigMaster91 wrote: ↑Fri Dec 15, 2017 6:14 pm
...
local square = {}
speed = 40
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
Re: Game with separate files tryout
Posted: Fri Dec 15, 2017 6:55 pm
by SynfigMaster91
xNick1 wrote: ↑Fri Dec 15, 2017 6:54 pm
MadByte wrote: ↑Fri Dec 15, 2017 6:47 pm
SynfigMaster91 wrote: ↑Fri Dec 15, 2017 6:14 pm
...
local square = {}
speed = 40
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
Then this doesn't help...
One more strike and I leave LÖVE and Lua forever!