Page 1 of 1
Making a player.
Posted: Sat Dec 14, 2013 7:03 pm
by Love2dNewbie
I'm making my very first game with Löve 2D and I'm having a few problems.
How do I change the x position of the player by 1 when he presses the letter d?
I tried:
Code: Select all
function love.update(dt)
if love.keyboard.isDown("d") then
player.x = player.x + 1
end
end
I'm a newbie to all this programming, so some help would be greatly appriciated.
Re: Making a player.
Posted: Sat Dec 14, 2013 7:16 pm
by Robin
Hi! You could try:
Code: Select all
function love.keypressed(key)
if key == 'd' then
player.x = player.x + 1
end
end
If that doesn't work for you, please upload a .love of your game so we can help you.
Re: Making a player.
Posted: Sat Dec 14, 2013 7:26 pm
by Love2dNewbie
Error: main.lua:23: attempt to perform arithmetic on field 'x' (a nil value)
[]
Posted: Sat Dec 14, 2013 7:29 pm
by bekey
-snip-
Re: Making a player.
Posted: Sat Dec 14, 2013 7:32 pm
by Love2dNewbie
how can i define it?
Re: Making a player.
Posted: Sat Dec 14, 2013 7:44 pm
by Chroteus
Post .love file or the code.
Re: Making a player.
Posted: Sat Dec 14, 2013 7:44 pm
by MadByte
You can create a table for all variables belong to the player.
Code: Select all
-- main.lua
function love.load()
player = {} -- creates a table
player.x = 10 -- adds the variable "x" to the table.
player.y = 10 -- adds the variable "y" to the table.
player.vel = 100 -- ...
end
function love.update(dt)
if love.keyboard.isDown("left") then
player.x = player.x - player.vel * dt
end
end
EDIT: This is meant for continiously applied velocity to the player coord's.
Robin posted a option for adding a value each time a key is pressed - but not hold down.
Re: Making a player.
Posted: Sat Dec 14, 2013 7:48 pm
by Love2dNewbie
Still doesnt work.
My code:
Code: Select all
function love.load()
player = {}
player.x = 10
player.y = 276
player.vel = 1
print('Game Loaded')
print('v 0.1')
love.graphics.setBackgroundColor(0, 255, 255)
player = love.graphics.newImage("textures/char.png")
end
function love.draw()
love.graphics.setColor(103, 164, 21, 255)
love.graphics.rectangle("fill", 0, 300, 800, 300)
love.graphics.setColor(240, 255, 255, 255)
love.graphics.draw(player, 10, 276, 0, 1)
end
function love.update(dt)
if love.keyboard.isDown("left") then
player.x = player.x - player.vel * dt
end
end
function love.keypressed(key, unicode)
end
function love.keyreleased(key, unicode)
end
Re: Making a player.
Posted: Sat Dec 14, 2013 7:52 pm
by Chroteus
Code: Select all
player = love.graphics.newImage("textures/char.png")
Here's your problem. First you define player as a table, and then as an image.
Define image with player.image, not player variable.
And in draw function, draw player.image, instead of player.
So, your game should look like this:
Code: Select all
function love.load()
player = {}
player.x = 10
player.y = 276
player.vel = 1
player.image = love.graphics.newImage("textures/char.png")
print('Game Loaded')
print('v 0.1')
love.graphics.setBackgroundColor(0, 255, 255)
end
function love.draw()
love.graphics.setColor(103, 164, 21, 255)
love.graphics.rectangle("fill", 0, 300, 800, 300)
love.graphics.setColor(240, 255, 255, 255)
love.graphics.draw(player.image, 10, 276, 0, 1)
end
function love.update(dt)
if love.keyboard.isDown("left") then
player.x = player.x - player.vel * dt
end
end
function love.keypressed(key, unicode)
end
function love.keyreleased(key, unicode)
end
Re: Making a player.
Posted: Sat Dec 14, 2013 7:53 pm
by MadByte
Code: Select all
function love.draw()
love.graphics.setColor(103, 164, 21, 255)
love.graphics.rectangle("fill", 0, 300, 800, 300)
love.graphics.setColor(240, 255, 255, 255)
love.graphics.draw(player, 10, 276, 0, 1) -- Input variables here
end
Also you didn't associated the variables of the player to the image in your love.graphics.draw function. It should look like this:
Code: Select all
love.graphics.draw(player.img, player.x, player.y, 0, 1)
also put your player image inside the table for better readability. I usually add a "img" variable into the table.
Good luck