Working on my first project without a tutorial, and it was all going well until I was trying to add in code to prevent the player from entering an area where another object is.
--When I take out the world:update() out of love.main, the game loads fine.
--If I remove the world:bounds() the game will load but will crash as soon as I try to move.
Otherwise I get the error that my file is packaged incorrectly (I don't think this is the issue because it will run without the section of code mentioned above, and I've made a few other projects with the same folder format and not had this issue).
Here's the relevant bits of code(I'm new to this, and I know I can use physics to make this easier, but trying to do it the long way first).
world.lua
Code: Select all
world = {}
function world:load()
self:objects()
self:coordinates()
end
function world:update(dt)
self:objects()
self:bounds()
self:move()
end
function world:draw()
love.graphics.line(corner.wall.lefttop.x, corner.wall.lefttop.y, corner.wall.leftbottom.x, corner.wall.leftbottom.y)
love.graphics.line(corner.wall.lefttop.x, corner.wall.lefttop.y, corner.wall.righttop.x, corner.wall.righttop.y)
love.graphics.line(corner.wall.righttop.x, corner.wall.righttop.y, corner.wall.rightbottom.x, corner.wall.rightbottom.y)
love.graphics.line(corner.wall.leftbottom.x, corner.wall.leftbottom.y, corner.wall.rightbottom.x, corner.wall.rightbottom.y)
love.graphics.line(corner.wall.lefttop.x, corner.wall.lefttop.y, 0, 0)
love.graphics.line( corner.wall.righttop.x, corner.wall.righttop.y, corner.screen.right.x, 0)
love.graphics.line(corner.screen.right.x, corner.screen.bottom.y, corner.wall.rightbottom.x, corner.wall.rightbottom.y)
love.graphics.line(corner.wall.leftbottom.x, corner.wall.leftbottom.y, 0, corner.screen.bottom.y)
love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
love.graphics.rectangle("line", table.x, table.y, table.width, table.height)
end
function world:objects()
player = {}
player.height = 50
player.width = 25
player.speed = 200
player.diagonalspeed = ((player.speed ^ 2) / 2) ^ 0.5
player.x = (love.graphics.getWidth() / 2) - player.width
player.y = (love.graphics.getHeight() * 3 / 4) - player.height
table = {}
table.width = 125
table.height = 75
table.x = (love.graphics.getWidth()/2) - table.width
table.y = (love.graphics.getHeight()/2) - table.height
end
function world:coordinates()
corner = {wall = {lefttop = {}, leftbottom = {}, righttop = {}, rightbottom = {}},
screen = {bottom = {}, right = {}}
}
corner.screen.bottom.y = love.graphics.getHeight()
corner.screen.right.x = love.graphics.getWidth()
corner.wall.lefttop.x = love.graphics.getWidth() / 16
corner.wall.lefttop.y = love.graphics.getHeight() / 9
corner.wall.leftbottom.x = love.graphics.getWidth() / 16
corner.wall.leftbottom.y = (love.graphics.getHeight() * 8) / 9
corner.wall.righttop.x = (love.graphics.getWidth() * 15)/ 16
corner.wall.righttop.y = love.graphics.getHeight() / 9
corner.wall.rightbottom.x = (love.graphics.getWidth() * 15)/ 16
corner.wall.rightbottom.y = (love.graphics.getHeight() * 8) / 9
end
function world:move()
if love.keyboard.isDown("a") and love.keyboard.isDown("s") then
player.x = player.x - (player.diagonalspeed * dt)
player.y = player.y + (player.diagonalspeed * dt)
elseif love.keyboard.isDown("a") and love.keyboard.isDown("w") then
player.x = player.x - (player.diagonalspeed * dt)
player.y = player.y - (player.diagonalspeed * dt)
elseif love.keyboard.isDown("d") and love.keyboard.isDown("s") then
player.x = player.x + (player.diagonalspeed * dt)
player.y = player.y + (player.diagonalspeed * dt)
elseif love.keyboard.isDown("d") and love.keyboard.isDown("w") then
player.x = player.x + (player.diagonalspeed * dt)
player.y = player.y - (player.diagonalspeed * dt)
elseif love.keyboard.isDown("a") and love.keyboard.isDown("s") == false then
player.x = player.x - (player.speed * dt)
player.y = player.y
elseif love.keyboard.isDown("a") and love.keyboard.isDown("w") == false then
player.x = player.x - (player.speed * dt)
player.y = player.y
elseif love.keyboard.isDown("d") and love.keyboard.isDown("s") == false then
player.x = player.x + (player.speed * dt)
player.y = player.y
elseif love.keyboard.isDown("d") and love.keyboard.isDown("w") == false then
player.x = player.x + (player.speed * dt)
player.y = player.y
elseif love.keyboard.isDown("w") and love.keyboard.isDown("d") == false then
player.y = player.y - (player.speed * dt)
player.x = player.x
elseif love.keyboard.isDown("w") and love.keyboard.isDown("a") == false then
player.y = player.y - (player.speed * dt)
player.x = player.x
elseif love.keyboard.isDown("s") and love.keyboard.isDown("d") == false then
player.y = player.y + (player.speed * dt)
player.x = player.x
elseif love.keyboard.isDown("s") and love.keyboard.isDown("a") == false then
player.y = player.y + (player.speed * dt)
player.x = player.x
end
end
function world:bounds()
if player.x < (love.graphics.getWidth() / 16) then
player.x = (love.graphics.getWidth() / 16)
end
if player.x > ((love.graphics.getWidth() * 15/ 16) - player.width) then
player.x = ((love.graphics.getWidth() *15 / 16) - player.width)
end
if player.y < ((love.graphics.getHeight() / 9) - (player.height / 3)) then
player.y = ((love.graphics.getHeight() / 9) - (player.height / 3))
end
if player.y > ((love.graphics.getHeight() * 8 / 9) - player.height)then
player.y = ((love.graphics.getHeight() * 8 / 9) - player.height)
end
if player.x > ((love.graphics.getWidth() * 15/ 16) - player.width)then
player.x = ((love.graphics.getWidth() *15 / 16) - player.width)
end
if player.y < ((love.graphics.getHeight() / 9) - (player.height / 3)) then
player.y = ((love.graphics.getHeight() / 9) - (player.height / 3))
end
if player.y > ((love.graphics.getHeight() * 8 / 9) - player.height)then
player.y = ((love.graphics.getHeight() * 8 / 9) - player.height)
end
if (player.x + player.width) > (table.x) and player.y >= table.y and player.y <= (table.y + table.height) then
player.x = table.x - player.width
end
if player.x < (table.x + table.width) and player.y >= table.y and player.y <= (table.y + table.height) then
player.x = table.x + table.width
end
if player.y > table.y and player.x >= table.x and player.x <= (table.s + table.width) then
player.y = table.y - player.height
end
if player.y < table.y + table.height and player.x >= table.x and player.x <= (table.s + table.width) then
player.y = (table.y + table.height) - (player.height / 3)
end
end