Can someone help me with this piece of code?
Posted: Wed Nov 11, 2020 9:53 pm
Hello guys! New to Love2D; somewhat familiar with Lua (used Pico8 for last year's Game Off). I'm using Love2D this year for Game Of 2020 and I'm pretty excited to use a language/framework I'm somewhat familiar with.
I'm going through the documentation right now and I'm having some trouble. I followed the animation one and I got it work and I found the basic platformer one for basic movement and I kinda got that working.
The problem is all the sprites from the spritesheet shows when I try to just load as it is; (also when I reference the animation variable I get a draw error so I have to use the static image which gives me the whole sheet).
Is there a way I can just load the first pic from the sprite sheet? Also follow-up question; How do I make the animation work with movement?
Current code I have;
I'm going through the documentation right now and I'm having some trouble. I followed the animation one and I got it work and I found the basic platformer one for basic movement and I kinda got that working.
The problem is all the sprites from the spritesheet shows when I try to just load as it is; (also when I reference the animation variable I get a draw error so I have to use the static image which gives me the whole sheet).
Is there a way I can just load the first pic from the sprite sheet? Also follow-up question; How do I make the animation work with movement?
Current code I have;
Code: Select all
platform = {}
player = {}
function love.load()
animation = newAnimation(love.graphics.newImage("assets/linus.png"), 32, 32, 0.8)
animation1 = newAnimation(love.graphics.newImage("assets/city.png"), 8, 8, 0.8)
platform.width = love.graphics.getWidth()
platform.height = love.graphics.getHeight()
platform.x = 0
platform.y = platform.height / 2
player.x = love.graphics.getWidth() / 2
player.y = love.graphics.getHeight() / 2
player.speed = 200
player.img = love.graphics.newImage("assets/linus.png")
player.ground = player.y
player.y_velocity = 0
player.jump_height = -300
player.gravity = -500
end
function love.update(dt)
keys(dt)
end
function love.draw()
-- local spriteNum = math.floor(animation.currentTime / animation.duration * #animation.quads) + 1
-- love.graphics.draw(animation.spriteSheet, animation.quads[spriteNum], 0, 10, 0, 4)
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle('fill', platform.x, platform.y, platform.width, platform.height)
love.graphics.draw(player.img, player.x, player.y, 0, 1, 1, 0, 32)
end
function newAnimation(image, width, height, duration)
local animation = {}
animation.spriteSheet = image;
animation.quads = {};
for y = 0, image:getHeight() - height, height do
for x = 0, image:getWidth() - width, width do
table.insert(animation.quads, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
end
end
animation.duration = duration or 1
animation.currentTime = 0
return animation
end
function keys(dt)
if love.keyboard.isDown('d') then
if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
player.x = player.x + (player.speed * dt)
end
elseif love.keyboard.isDown('a') then
if player.x > 0 then
player.x = player.x - (player.speed * dt)
end
end
if love.keyboard.isDown('space') then
if player.y_velocity == 0 then
player.y_velocity = player.jump_height
end
end
if player.y_velocity ~= 0 then
player.y = player.y + player.y_velocity * dt
player.y_velocity = player.y_velocity - player.gravity * dt
end
if player.y > player.ground then
player.y_velocity = 0
player.y = player.ground
end
end