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
Code: Select all
function love.update(dt)
if love.keyboard.isDown("d") then
player.x = player.x + 1
end
end
Code: Select all
function love.keypressed(key)
if key == 'd' then
player.x = player.x + 1
end
end
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
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
Code: Select all
player = love.graphics.newImage("textures/char.png")
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
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
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
Code: Select all
love.graphics.draw(player.img, player.x, player.y, 0, 1)
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 6 guests