Brief explanation of project: I have been playing a lot of Persona 5 and wanted to create a simple prototype game with an "overworld" in which you can move around and a classic JRPG "turn-based battle" mode that occurs when you run into an enemy.
Intended result: In "battle mode", player can use arrow keys to move a selector around a menu and choose an action with "enter" key (not yet implemented except for "Give Up" to exit battle).
Problem: After entering battle mode, I cannot move the selector around the menu with the arrow keys. Nothing happens. The strange thing is that if I set battle = true in the code and start the game in battle mode, I can move the selector around the menu using the arrow keys. However if I then select "give up" and re-enter the battle, the same problem will occur. I suspect that I am misunderstanding something about love.keypressed. Thanks for your help!
Code: Select all
function love.load()
love.graphics.setBackgroundColor(135/255, 206/255, 235/255)
player = {}
enemy = {}
player.width = 10
player.height = 20
player.x = 10
player.y = 300 - player.height
player.speed =1
enemy.width = 15
enemy.height = 40
enemy.x = 150
enemy.y = 300 - enemy.height
battle = true
select = {}
select.position = 1
select.width = 20
select.height = 10
select.x = 350 + 112 - select.width - 2
select.y = 450 + 2
end
function love.update()
if battle == false then
worldUpdate()
end
end
function love.draw()
if battle == false then
worldDraw()
love.graphics.print("battle = false", 100, 100)
else
battleDraw()
love.graphics.print("battle = true", 100, 100)
end
end
function love.keypressed(key)
if battle == true then
if key == "right" then
if select.position == 1 or select.position == 3 then
select.position = select.position + 1
select.x = select.x + 225
else
select.position = select.position - 1
select.x = select.x - 225
end
end
if key == "left" then
if select.position == 2 or select.position == 4 then
select.position = select.position - 1
select.x = select.x - 225
else
select.position = select.position + 1
select.x = select.x + 225
end
end
if key == "down" then
if select.position == 1 or select.position == 2 then
select.position = select.position + 2
select.y = select.y + 100
else
select.position = select.position - 2
select.y = select.y - 100
end
end
if key == "up" then
if select.position == 3 or select.position == 4 then
select.position = select.position - 2
select.y = select.y - 100
else
select.position = select.position + 2
select.y = select.y + 100
end
end
if key == "return" then
if select.position == 1 then
--Attack
elseif select.position == 2 then
--Prepare
elseif select.position == 3 then
--Item
elseif select.position == 4 then
--Give Up
battle = false
end
end
end
end
function worldUpdate()
if love.keyboard.isDown("right") then
player.x = player.x + player.speed
end
if love.keyboard.isDown("left") then
player.x = player.x - player.speed
end
if checkCollision(player, enemy) == true then
battle = "true"
player.x = 100
end
end
function worldDraw()
--platform
love.graphics.rectangle("fill", 0, 300, 800, 600)
--player
love.graphics.rectangle("line", player.x, player.y, player.width, player.height)
--enemy
love.graphics.rectangle("line", 150, 260, 15, 40)
end
function battleDraw()
love.graphics.setColor(255, 255, 255)
--player
love.graphics.rectangle("line", 50, 250, player.width * 25, player.height * 25)
--enemy
love.graphics.rectangle("line", 600, 150, enemy.width * 5, enemy.height * 5)
--box
love.graphics.rectangle("fill", 350, 400, 450, 200)
--text
love.graphics.setColor(0, 0, 0)
love.graphics.print("Attack", 350 + 112, 450)
love.graphics.print("Item", 350 + 112, 550)
love.graphics.print("Prepare", 350 + 337, 450)
love.graphics.print("Give Up", 350 + 337, 550)
--selector
love.graphics.rectangle("fill", select.x, select.y, select.width, select.height)
love.graphics.print(select.position, 100, 120)
end
function checkCollision(a, b)
local a_left = a.x
local a_right = a.x + a.width
local a_top = a.y
local a_bottom = a.y + a.height
local b_left = b.x
local b_right = b.x + b.width
local b_top = b.y
local b_bottom = b.y + b.height
if a_right > b_left and
a_left < b_right and
a_bottom > b_top and
a_top < b_bottom then
return true
else
return false
end
end