如何使love2d游戏兼容

时间:2017-05-16 10:54:14

标签: lua love2d

我收到消息“这个游戏表明它是为了爱的版本'0.9.1'而制作的。 它可能与正在运行的版本(0.10.2)不兼容。“当我尝试运行我的游戏时。游戏仍然可以工作,但是这条消息让我烦恼。如何将其更新到最新版本?我的代码在这里: debug = true 的 Main.lua      - 计时器

-- We declare these here so we don't have to edit them multiple places
canShoot = true
canShootTimerMax = 0.2 
canShootTimer = canShootTimerMax
createEnemyTimerMax = 0.4
createEnemyTimer = createEnemyTimerMax

-- Player Object
player = { x = 200, y = 710, speed = 150, img = nil }
isAlive = true
score  = 0

-- Image Storage
bulletImg = nil
enemyImg = nil

-- Entity Storage
bullets = {} -- array of current bullets being drawn and updated
enemies = {} -- array of current enemies on screen

-- Collision detection taken function from http://love2d.org/wiki/BoundingBox.lua
-- Returns true if two boxes overlap, false if they don't
-- x1,y1 are the left-top coords of the first box, while w1,h1 are its width and height
-- x2,y2,w2 & h2 are the same, but for the second box
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end

-- Loading
function love.load(arg)
    player.img = love.graphics.newImage('assets/plane.png')
    enemyImg = love.graphics.newImage('assets/enemy.png')
    bulletImg = love.graphics.newImage('assets/bullet.png')
end


-- Updating
function love.update(dt)
    -- I always start with an easy way to exit the game
    if love.keyboard.isDown('escape') then
        love.event.push('quit')
    end

    -- Time out how far apart our shots can be.
    canShootTimer = canShootTimer - (1 * dt)
    if canShootTimer < 0 then
        canShoot = true
    end

    -- Time out enemy creation
    createEnemyTimer = createEnemyTimer - (1 * dt)
    if createEnemyTimer < 0 then
        createEnemyTimer = createEnemyTimerMax

        -- Create an enemy
        randomNumber = math.random(10, love.graphics.getWidth() - 10)
        newEnemy = { x = randomNumber, y = -10, img = enemyImg }
        table.insert(enemies, newEnemy)
    end


    -- update the positions of bullets
    for i, bullet in ipairs(bullets) do
        bullet.y = bullet.y - (250 * dt)

        if bullet.y < 0 then -- remove bullets when they pass off the screen
            table.remove(bullets, i)
        end
    end

    -- update the positions of enemies
    for i, enemy in ipairs(enemies) do
        enemy.y = enemy.y + (200 * dt)

        if enemy.y > 850 then -- remove enemies when they pass off the screen
            table.remove(enemies, i)
        end
    end

    -- run our collision detection
    -- Since there will be fewer enemies on screen than bullets we'll loop them first
    -- Also, we need to see if the enemies hit our player
    for i, enemy in ipairs(enemies) do
        for j, bullet in ipairs(bullets) do
            if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), bullet.x, bullet.y, bullet.img:getWidth(), bullet.img:getHeight()) then
                table.remove(bullets, j)
                table.remove(enemies, i)
                score = score + 1
            end
        end

        if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), player.x, player.y, player.img:getWidth(), player.img:getHeight()) 
        and isAlive then
            table.remove(enemies, i)
            isAlive = false
        end
    end


    if love.keyboard.isDown('left','a') then
        if player.x > 0 then -- binds us to the map
            player.x = player.x - (player.speed*dt)
        end
    elseif love.keyboard.isDown('right','d') then
        if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
            player.x = player.x + (player.speed*dt)
        end
    end

    if love.keyboard.isDown(' ', 'rctrl', 'lctrl', 'ctrl') and canShoot then
        -- Create some bullets
        newBullet = { x = player.x + (player.img:getWidth()/2), y = player.y, img = bulletImg }
        table.insert(bullets, newBullet)
        canShoot = false
        canShootTimer = canShootTimerMax
    end

    if not isAlive and love.keyboard.isDown('r') then
        -- remove all our bullets and enemies from screen
        bullets = {}
        enemies = {}

        -- reset timers
        canShootTimer = canShootTimerMax
        createEnemyTimer = createEnemyTimerMax

        -- move player back to default position
        player.x = 50
        player.y = 710

        -- reset our game state
        score = 0
        isAlive = true
    end
end

-- Drawing
function love.draw(dt)
    for i, bullet in ipairs(bullets) do
        love.graphics.draw(bullet.img, bullet.x, bullet.y)
    end

    for i, enemy in ipairs(enemies) do
        love.graphics.draw(enemy.img, enemy.x, enemy.y)
    end

    love.graphics.setColor(255, 255, 255)
    love.graphics.print("SCORE: " .. tostring(score), 400, 10)

    if isAlive then
        love.graphics.draw(player.img, player.x, player.y)
    else
        love.graphics.print("Press 'R' to restart", love.graphics:getWidth()/2-50, love.graphics:getHeight()/2-10)
    end

    if debug then
        fps = tostring(love.timer.getFPS())
        love.graphics.print("Current FPS: "..fps, 9, 10)
    end
end

conf.lua

-- Configuration
function love.conf(t)
    t.title = "Scrolling Shooter Tutorial" -- The title of the window the game is in (string)
    t.version = "0.9.1"         -- The LÖVE version this game was made for (string)
    t.window.width = 480        -- we want our game to be long and thin.
    t.window.height = 800

    -- For Windows debugging
    t.console = true
end

1 个答案:

答案 0 :(得分:3)

正如评论中所述,您的案例中的问题是,在conf.lua中,版本被指定为"0.9.1"。在某些情况下,将此值更改为"0.10.2"就足够了,但在0.9.0和0.10.0之间发生了大量更改。

要特别注意鼠标输入 肯定 会被破坏,因为在0.10.0之前的版本中,LOVE使用 strings 来表示鼠标按钮,而在0.10.0及更高版本中,使用数字。要解决此问题,请查找与鼠标相关的功能(love.mouse.isDownlove.mousepressed等)并将"l"更改为1"r"更改为{{1} }, 等等。有关详情,请参阅old valueslove.mousepressed的完整列表。此外,鼠标滚轮移动也发生了变化,添加了love.wheelmoved回调并删除了传递给2的字符串。

此外,请仔细阅读changelog以了解可能影响您的计划的所有更改。

相关问题