Override a current button?
Posted: Thu Nov 08, 2018 1:49 pm
So below is the code for my project's character animation and move function. When I press the d key the character moves right and the animation follows. When you hold the d key and then the a key the character moves to the left. When I try the same but press the a key first the character doesn't switch directions after I press and hold d too. Is there a way of fixing this without drifting too far from my original data structure? I also would like to know how to make the move function so that the character can move both right and up at the same time(e.g) so that it moves laterally.
Oh and btw, the arms and body are animated separately so the character can run and slash his sword at the same time. (if you were wondering)
Thanks for having a look,
xpali2
edit: I already seem to know that this is due to the order of draw statements in player_draw() but would like to keep the function this way
Oh and btw, the arms and body are animated separately so the character can run and slash his sword at the same time. (if you were wondering)
Thanks for having a look,
xpali2
edit: I already seem to know that this is due to the order of draw statements in player_draw() but would like to keep the function this way
Code: Select all
local frameTime = 0
local frameArmTime = 0
function player_load()
idle = love.graphics.newImage("Player/KnightlyIdle.png")
unarmedRun = love.graphics.newImage("Player/Knightlyunarmedrunningcycle.png")
armedRun = love.graphics.newImage("Player/Knightlyarmedruncycle.png")
verticalRun = love.graphics.newImage("Player/KnightlyFrontandBackRunAnimation.png")
attackblockArms = love.graphics.newImage("Player/Knightlyattackandblockarms.png")
player = {
Animations = {
idleQuads = {
love.graphics.newQuad(0, 0, 32, 32, idle:getDimensions()),
love.graphics.newQuad(32, 0, 32, 32, idle:getDimensions()),
love.graphics.newQuad(64, 0, 32, 32, idle:getDimensions()),
love.graphics.newQuad(96, 0, 32, 32, idle:getDimensions())
},
idleArmQuads = {
love.graphics.newQuad(0, 64, 32, 32, idle:getDimensions()),
love.graphics.newQuad(32, 64, 32, 32, idle:getDimensions()),
love.graphics.newQuad(64, 64, 32, 32, idle:getDimensions()),
love.graphics.newQuad(96, 64, 32, 32, idle:getDimensions())
},
idleArmedQuads = {
love.graphics.newQuad(0, 32, 32, 32, idle:getDimensions()),
love.graphics.newQuad(32, 32, 32, 32, idle:getDimensions()),
love.graphics.newQuad(64, 32, 32, 32, idle:getDimensions()),
love.graphics.newQuad(96, 32, 32, 32, idle:getDimensions())
},
idleArmedArmQuads= {
love.graphics.newQuad(0, 96, 32, 32, idle:getDimensions()),
love.graphics.newQuad(32, 96, 32, 32, idle:getDimensions()),
love.graphics.newQuad(64, 96, 32, 32, idle:getDimensions()),
love.graphics.newQuad(96, 96, 32, 32, idle:getDimensions())
},
runQuads = {
love.graphics.newQuad(0, 0, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(32, 0, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(64, 0, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(96, 0, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(0, 32, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(32, 32, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(64, 32, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(96, 32, 32, 32, unarmedRun:getDimensions())
},
runQuadsArms = {
love.graphics.newQuad(0, 64, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(32, 64, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(64, 64, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(96, 64, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(0, 96, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(32, 96, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(64, 96, 32, 32, unarmedRun:getDimensions()),
love.graphics.newQuad(96, 96, 32, 32, unarmedRun:getDimensions())
},
armedRunQuads = {
love.graphics.newQuad(0, 0, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(32, 0, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(64, 0, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(96, 0, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(0, 32, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(32, 32, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(64, 32, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(96, 32, 32, 32, armedRun:getDimensions())
},
armedRunQuadsArms = {
love.graphics.newQuad(0, 64, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(32, 64, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(64, 64, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(96, 64, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(0, 96, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(32, 96, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(64, 96, 32, 32, armedRun:getDimensions()),
love.graphics.newQuad(96, 96, 32, 32, armedRun:getDimensions())
},
vRunQuadsDown = {
love.graphics.newQuad(0, 0, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(32, 0, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(64, 0, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(96, 0, 32, 32, verticalRun:getDimensions())
},
vRunQuadsUp = {
love.graphics.newQuad(0, 64, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(32, 64, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(64, 64, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(96, 64, 32, 32, verticalRun:getDimensions())
},
vArmedRunQuadsDown = {
love.graphics.newQuad(0, 32, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(32, 32, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(64, 32, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(96, 32, 32, 32, verticalRun:getDimensions())
},
vArmedRunQuadsUp = {
love.graphics.newQuad(0, 96, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(32, 96, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(64, 96, 32, 32, verticalRun:getDimensions()),
love.graphics.newQuad(96, 96, 32, 32, verticalRun:getDimensions())
},
blockArms = {
love.graphics.newQuad(0, 32, 32, 32, attackblockArms:getDimensions()),
love.graphics.newQuad(32, 32, 32, 32, attackblockArms:getDimensions()),
love.graphics.newQuad(64, 32, 32, 32, attackblockArms:getDimensions()),
love.graphics.newQuad(96, 32, 32, 32, attackblockArms:getDimensions())
},
attackArms = {
love.graphics.newQuad(0, 0, 32, 32, attackblockArms:getDimensions()),
love.graphics.newQuad(32, 0, 32, 32, attackblockArms:getDimensions()),
love.graphics.newQuad(64, 0, 32, 32, attackblockArms:getDimensions()),
love.graphics.newQuad(96, 0, 32, 32, attackblockArms:getDimensions())
},
},
currentQuad = 1,
currentArmQuad = 1,
x = 400,
y = 300,
xoffset = 16,
yoffset = 16,
hp = 100,
dmg = 25,
status = "idle",
armStatus = "idle",
isArmed = false,
yscale = 1,
xscale = 1,
speed = 75
}
end
function player_draw()
if player.status == "idle" then
love.graphics.draw(idle, player.Animations.idleQuads[player.currentQuad], player.x - player.xoffset, player.y - player.yoffset, 0, player.xscale, player.yscale)
love.graphics.draw(idle, player.Animations.idleArmQuads[player.currentArmQuad], player.x - player.xoffset, player.y - player.yoffset, 0, player.xscale, player.yscale)
elseif player.status == "runleft" then
love.graphics.draw(unarmedRun, player.Animations.runQuads[player.currentQuad], player.x - player.xoffset, player.y - player.yoffset, 0, player.xscale, player.yscale)
love.graphics.draw(unarmedRun, player.Animations.runQuadsArms[player.currentArmQuad], player.x - player.xoffset, player.y - player.yoffset, 0, player.xscale, player.yscale)
elseif player.status == "runright" then
love.graphics.draw(unarmedRun, player.Animations.runQuads[player.currentQuad], player.x - player.xoffset, player.y - player.yoffset, 0, player.xscale, player.yscale)
love.graphics.draw(unarmedRun, player.Animations.runQuadsArms[player.currentArmQuad], player.x - player.xoffset, player.y - player.yoffset, 0, player.xscale, player.yscale)
end
end
function player_update(dt)
if love.keyboard.isDown("a") then
player.x = player.x - player.speed * dt
elseif love.keyboard.isDown("d") then
player.x = player.x + player.speed * dt
end
if love.keyboard.isDown("a") then
if player.isArmed == false then
player.status = "runleft"
player.xscale = -1
player.xoffset = -16
elseif player.isArmed == true then
player.status = "runleftarmed"
player.xscale = -1
player.xoffset = -16
end
elseif love.keyboard.isDown("w") then
elseif love.keyboard.isDown("s") then
elseif love.keyboard.isDown("d") then
if player.isArmed == false then
player.status = "runright"
player.xscale = 1
player.xoffset = 16
elseif player.isArmed == true then
player.status = "runrightarmed"
player.xscale = 1
player.xoffset = 16
end
else player.status = "idle"
end
if player.status == "runleft" then
frameTime = frameTime + dt
for i, v in pairs(player.Animations.runQuads) do
if frameTime > 0.1 then
player.currentQuad = player.currentQuad + 1
frameTime = 0
elseif player.currentQuad > 8 then
player.currentQuad = 1
end
end
end
if player.status == "runleft" then
frameArmTime = frameArmTime + dt
for i, v in pairs(player.Animations.runQuadsArms) do
if frameArmTime > 0.1 then
player.currentArmQuad = player.currentArmQuad + 1
frameArmTime = 0
elseif player.currentArmQuad > 8 then
player.currentArmQuad = 1
end
end
end
if player.status == "runright" then
frameTime = frameTime + dt
for i, v in pairs(player.Animations.runQuads) do
if frameTime > 0.1 then
player.currentQuad = player.currentQuad + 1
frameTime = 0
elseif player.currentQuad > 8 then
player.currentQuad = 1
end
end
end
if player.status == "runright" then
frameArmTime = frameArmTime + dt
for i, v in pairs(player.Animations.runQuadsArms) do
if frameArmTime > 0.1 then
player.currentArmQuad = player.currentArmQuad + 1
frameArmTime = 0
elseif player.currentArmQuad > 8 then
player.currentArmQuad = 1
end
end
end
if player.status == "idle" then
frameTime = frameTime + dt
for i, v in pairs(player.Animations.idleQuads) do
if frameTime > 0.5 then
player.currentQuad = player.currentQuad + 1
frameTime = 0
elseif player.currentQuad > 4 then
player.currentQuad = 1
end
end
end
if player.status == "idle" then
frameArmTime = frameArmTime + dt
for i, v in pairs(player.Animations.idleArmQuads) do
if frameArmTime > 0.5 then
player.currentArmQuad = player.currentArmQuad + 1
frameArmTime = 0
elseif player.currentArmQuad > 4 then
player.currentArmQuad = 1
end
end
end
end
function player_keypressed(key)
end
function player_mousepressed(x, y, button)
end