Simple animation function doesn't work
Posted: Sat Jul 16, 2016 7:16 pm
Hi everyone,
I am trying to make a simple animation function. At first the function turned the variables that I put in, into strings somehow. I have solved this problem with the solution given to me on stackoverflow: http://stackoverflow.com/questions/3841 ... -in-love2d.
Now my problem is that only the first image is shown and not second. His upper body should move slightly up and down.
Does anybody know how I can improve my code?
Controls:
3 - Go to the game state
w/up - Move the player up
a/left - Move the player left
d/right - Move the player right
s/down - Move the player down
Code:
I am trying to make a simple animation function. At first the function turned the variables that I put in, into strings somehow. I have solved this problem with the solution given to me on stackoverflow: http://stackoverflow.com/questions/3841 ... -in-love2d.
Now my problem is that only the first image is shown and not second. His upper body should move slightly up and down.
Does anybody know how I can improve my code?
Controls:
3 - Go to the game state
w/up - Move the player up
a/left - Move the player left
d/right - Move the player right
s/down - Move the player down
Code:
Code: Select all
function love.load()
--Makes images look pixelated
love.graphics.setDefaultFilter("nearest")
--Variables
player = {x = 10, y = 10}
--Animation
animate = {frame = nil}
function animate:animate(...)
local i = 1
self.frame = select(i, ...)
i = i + 1
if i < #arg then
i = 1
end
end
animateRizon = animate
rizon1 = love.graphics.newImage('rizonstand1.png')
rizon2 = love.graphics.newImage('rizonstand2.png')
animateRizon:animate(rizon1, rizon2)
--Gamestates
stateIntro = false
stateMenu = false
stateGame = false
end
function love.update(dt)
--Movement player
function movement()
if love.keyboard.isDown('escape') then
love.event.push('quit')
end
if love.keyboard.isDown('d', 'right') then
player.x = player.x + 1
elseif love.keyboard.isDown('a', 'left') then
player.x = player.x - 1
elseif love.keyboard.isDown('s', 'down') then
player.y = player.y + 1
elseif love.keyboard.isDown('w', 'up') then
player.y = player.y - 1
end
end
--Change states in the program
function stateChange()
if love.keyboard.isDown('escape') then
love.event.push('quit')
end
if love.keyboard.isDown('1') then
stateIntro = true
stateMenu = false
stateGame = false
elseif love.keyboard.isDown('2') then
stateIntro = false
stateMenu = true
stateGame = false
elseif love.keyboard.isDown('3') then
stateIntro = false
stateMenu = false
stateGame = true
end
end
end
function love.draw()
--Scales everything
love.graphics.scale(3, 3)
stateChange()
if stateIntro == true then
elseif stateMenu == true then
elseif stateGame == true then
love.graphics.setBackgroundColor(0, 0, 255)
love.graphics.draw(animateRizon.frame, player.x, player.y)
movement()
end
end