سؤال

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
هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

How about this update method:

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

Basically you want to say if a movement key is down and the object can move then move.

Also I would use the center of the sprite's bottom edge as the pivot point. Then you would need to use ox = 32 and ox = 64 (origin offset) assuming you have a 64 x 64 sprite.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top