2017-07-17 76 views
0

我是Löve的新手,對於任何錯誤我很抱歉,我可能已經說過了。 我見過Gridlocked Player教程,我想做一些基於他人的東西。這是一個看不見的迷宮,在你觸摸它們時畫出瓷磚。Love2d Gridlocked播放器問題

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

我想展示一些屏幕,這意味着當它離開迷宮時它是完整的,但我幾乎不知道如何解決這個問題。

+1

請將代碼粘貼到問題和格式正確。 – sudo

+0

請將代碼作爲文本粘貼到您的問題中,然後突出顯示並按下Ctrl + K,這樣我們就可以將您的代碼複製並粘貼到我們的IDE中,並幫助識別問題。請閱讀[如何創建一個最小,完整,可驗證的示例](https://stackoverflow.com/help/mcve)知道你需要包含哪些代碼 – WhatsThePoint

+0

感謝您的更正。 – Dayanne

回答

1

的問題是上線seen_tiles[(player.grid_y/32) + y][(player.grid_x/32) + x] = 1

您正在試圖改變這種狀況不存在,因爲你是離網的數組項。

嘗試防止播放器首先離開網格,或者將上面引用的代碼放入函數中,並在做任何事之前檢查數組中的項是否存在。

0

從數組中操作或讀取時,如果它存在,您應該檢查每個查找步驟,否則嘗試訪問不存在的東西時會出錯。 因此對於map[a][b]你檢查是否map[a] ~= nil and map[a][b] ~= nil然後宣佈地圖圖塊。如果您嘗試訪問map[a][b]mapmap[a]未被聲明,那麼在嘗試訪問不存在的內容時會出錯。 這適用於創建,讀取和更改數組及其內容。

例如:

添加新的陣列映射:if map[a] ~= nil then map[a][b] = {}; else map[a] = {}; map[a][b] = {}; end --here你是否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 --IF你嘗試尋找map[a][b],Love2d將嘗試訪問map,然後[a]裏面的地圖,那麼它看起來裏面是什麼[b]。如果在到達map[a][b]之前未能找到mapmap[a],而不是檢索nil,則會導致錯誤。