【问题标题】:Love2d Gridlocked Player IssuesLove2d 陷入僵局的播放器问题
【发布时间】:2017-07-18 12:31:58
【问题描述】:

我是 Löve 的新手,对于我可能已经说过的任何错误,我深表歉意。 我看过 Gridlocked Player 教程,我想根据另一个人做类似的事情。这是一个看不见的迷宫,当你触摸它们时会绘制它们。

local points = 0
width = love.graphics.getWidth()
height = love.graphics.getHeight()

function love.load()
    picture = love.graphics.newImage( "candy.png")
    back = love.graphics.newImage( "sky.jpg")
    cloud = love.graphics.newImage("cloud.png")
    love.mouse.setVisible(false)
    song = love.audio.newSource('elevator.mp3')
    song:setLooping(true)
    song:play()
    song:setVolume(0.1)
    anime = love.graphics.newImage("anime.png")
    music = love.audio.newSource("tick.mp3")
    music:setVolume(0.3)
    font = love.graphics.newFont("Kendal.ttf", 18)
    font1 = love.graphics.newFont("Kendal.ttf", 40)
    font2 = love.graphics.newFont("Kendal.ttf", 60)

    player = {
      grid_x = 256,
      grid_y = 256,
      act_x = 300,
      act_y = 300,
      speed = 20
    }

  map = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
    { 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
    { 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
    { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
  }

  -- Cria um array do mesmo tamanho do array de map
  seen_tiles = {}
  for yindex,column in ipairs(map) do
    seen_tiles[yindex] = {}
    for xindex,tile in ipairs(column) do
      seen_tiles[yindex][xindex] = 0
    end
  end

end

function love.update(dt)
  function testMap (x, y)
  if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
    -- Marca um bloco como visto assim que setá-lo para 1
    seen_tiles[(player.grid_y / 32) + y][(player.grid_x / 32) + x] = 1
    return false
  end
  return true
  end
  player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
  player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
end


function love.draw()    
  love.graphics.draw(back, 0, 0)
  love.graphics.setColor(3, 3, 3)
  love.graphics.rectangle("fill", 580, 0, width, height)
  love.graphics.setColor(255, 255, 255)
  love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
  love.graphics.draw(cloud, 591, 150, 0, 0.2111111)
  love.graphics.setFont(font1)
  love.graphics.print("Ghost Maze", 600, 200)
  love.graphics.setFont(font)
  love.graphics.print("Minimum movements: 12", 600, 280)
  love.graphics.print("Your movements:" .. points, 600, 300)
  love.graphics.draw( picture, love.mouse.getX() , love.mouse.getY() )
  love.graphics.draw(anime, 530, 360)

  -- Desenha os blocos se eles forem marcados como vistos
  for y=1, #seen_tiles do
        for x=1, #seen_tiles[y] do
            if seen_tiles[y][x] == 1 then
                love.graphics.setColor(120, 0, 200)
                love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
                love.graphics.setColor(255,255,255)
            end

        end
    end
end

function love.keypressed (key)
  if key == "escape" then
    love.event.quit()
  end
  if key == "up" then
    if testMap (0, -1) then
      player.grid_y = player.grid_y - 32
      points = points + 1
      love.audio.play(music)
    end
  elseif key == "down" then
    if testMap (0, 1) then
      player.grid_y = player.grid_y + 32
      points = points + 1
      love.audio.play(music)

    end
  elseif key == "left" then
    if testMap (-1, 0) then
      player.grid_x = player.grid_x - 32
      points = points + 1
      love.audio.play(music)

    end
  elseif key == "right" then
    if testMap (1, 0) then
      player.grid_x = player.grid_x + 32
      points = points + 1
      love.audio.play(music)

    end
  end
end

我的挣扎是,一旦角色(小盒子)离开“地图”,就会发生此错误:Error

我想显示一些屏幕,这意味着当它走出迷宫时关卡已完成,但我几乎不知道如何解决这个问题。

【问题讨论】:

  • 请将代码粘贴到问题中并正确格式化。
  • 请将代码作为文本粘贴到您的问题中,然后突出显示并按 Ctrl+K,这样我们就可以将您的代码复制并粘贴到我们的 IDE 中并帮助识别问题。请阅读how to create a Minimal, Complete, Verifiable example 了解您需要包含哪些代码
  • 感谢指正。
  • 可能map[(player.grid_y / 32) + y]nil

标签: lua love2d


【解决方案1】:

问题上线seen_tiles[(player.grid_y / 32) + y][(player.grid_x / 32) + x] = 1

您正在尝试更改一个不存在的数组项,因为您不在网格中。

尝试从一开始就阻止玩家离开网格,或者将我上面引用的代码变成一个函数,并在执行任何操作之前检查数组中的项目是否存在。

【讨论】:

    【解决方案2】:

    在操作或读取数组时,您应该检查每个查找步骤是否存在,否则在尝试访问不存在的内容时会出错。 所以对于map[a][b],您检查map[a] ~= nil and map[a][b] ~= nil 是否声明了地图图块。如果您在未声明 mapmap[a] 时尝试访问 map[a][b],则尝试访问不存在的内容时会出错。 这适用于创建、读取和更改数组及其内容。

    示例:

    将新数组添加到映射:if map[a] ~= nil then map[a][b] = {}; else map[a] = {}; map[a][b] = {}; end --在这里,您首先检查map[a] 是否存在,然后再声明map[a][b] 并创建以防它不存在,因为Love2d 将尝试访问map,然后首先访问map[a],然后然后在里面创建[b]。 如果找不到mapmap[a],而不是创建新的数组来插入,则会导致错误。

    在地图上查找值:if map[a] ~= nil then result = map[a][b]; else result = "map[a] doesnt exist so doesnt map[a][b]"; end --如果您尝试查找map[a][b],Love2d 将尝试访问map,然后在地图内部访问[a],然后它会查看[b] 内部的内容.如果在到达map[a][b] 之前未能找到mapmap[a],而不是检索nil,则会导致错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-14
      • 2014-10-21
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多