Search code examples
lualove2d

I need to limit the image movement LOVE2D


I'm using Love2d and Lua to make a game. Currently, I've got a guy, that 'glides' around, from left to right. I want to be able to limit his movement so he doesn't fall out of the screen. I tried making an if statement to detect if his X was larger than 800, (because my window size is 800x600) but it just ended up glitching out.. Here's the code. Please, help?

function love.load()
love.graphics.setBackgroundColor(92,217,255)
person={}
person.image=love.graphics.newImage('/sprites/spriteTest.png')
person.x=400
person.y=303
person.speed=200
hills=love.graphics.newImage('/sprites/spriteHills.png')
end
function love.update(dt)

if (person.x>735) then

    if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
        if (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
            person.x=person.x+(person.speed*dt)
        else
            person.x=person.x
        end

    elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
        if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
            person.x=person.x+(person.speed*dt)
        else
            person.x=person.x
        end

    end

elseif (person.x<0) then

    if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
        if (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
            person.x=person.x+(person.speed*dt)
        else
            person.x=person.x
        end

    elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
        if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
            person.x=person.x+(person.speed*dt)
        else
            person.x=person.x
        end

    end

else

    if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
        person.x=person.x+(person.speed*dt)
    elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
        person.x=person.x-(person.speed*dt)
    end

end

end
function love.draw()
love.graphics.draw(hills, 0, 0)
love.graphics.draw(person.image, person.x, person.y)
end

Solution

  • I found the answer. Here's the code

    function love.update(dt)
    
    if (player.x>735) then
    
        if (love.keyboard.isDown('left') or love.keyboard.isDown('a') or love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
            player.x=player.x-(player.speed*dt)
        end
    
    elseif (player.x<-10) then
    
        if (love.keyboard.isDown('left') or love.keyboard.isDown('a') or love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
            player.x=player.x+(player.speed*dt)
        end
    
    else
    
        if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
            person.x=person.x+(person.speed*dt)
        elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
            person.x=person.x-(person.speed*dt)
        end
    
    end
    
    end
    

    It might seem like it won't fix it, but it fixed it for me. I just set it that if he's at the edge, it won't let him go further.