如何在 löve2d 中停止滚动?

问题描述 投票:0回答:1

对于我的fnaf 粉丝游戏项目。我编写了一个原型。但我遇到了滚动问题,我试图阻止它,但我做不到。因此,如果有人可以提供帮助那就太好了!

这就是代码(只有更新和绘制函数)

function love.update(dt)
    player.x, player.y = love.mouse.getPosition()

    if reduce_back == 300 then
        reduce_back = reduce_back + 0
    end


    if player.x > 700 then             -- If the X position is over than 700 screen's pixel
        reduce_back = reduce_back + 25 -- There will have a scroll to the right
    elseif player.x < 60 then          -- If the X position is less than 60 screen's pixel
        reduce_back = reduce_back - 25 -- There will have a scroll to the left
    end
end

function love.draw()
    love.graphics.scale(1)
    -- Draws the background
    for i = 0, love.graphics.getWidth() / background:getWidth() do
        for j = 0, love.graphics.getHeight() / background:getHeight() do
            love.graphics.draw(
                background,
                i * background:getWidth() - reduce_back, -- reduce_back is the variable I use for setting the camera to the middle and I also use it for scrolling
                j * background:getHeight()
            )
        end
    end

    -- love.graphics.print(player.x, 1080 / 2, 0)
    -- love.graphics.print(player.y, 1080 / 2, 15)


    if bonnie.layers:getLayer() == 1 then
        love.graphics.scale(0.18)
        love.graphics.draw(bonnie.image, 0, 0)
        player:setLife()
    end

    if not player.isAlive then
        bonnie:playScream()
    end

    love.graphics.print(reduce_back)
end

如果您需要我的所有代码,如果您愿意,我可以发送给您。

lua scroll game-development love2d
1个回答
0
投票

如果你想制作一个可移动的背景(相机),那么你应该使用一个库,比如 hump(用于大规模进展的love2d超级实用程序)。

  camera = require 'lib/camera' --game Camera
  cam = camera() 
  function love.update(dt)
    cam:lookAt(player.x,player.y)
  end
  function love.draw()
    cam:attach()
     --[[all the things that will moveable 
      object on screen(like player,level)
     ]]--
    cam:detach
    --[[all the other things that is not to be 
     moved that is(score, ui,etc)
    ]]--

  end
© www.soinside.com 2019 - 2024. All rights reserved.