突出显示网格中的路径

Highlight a path in a grid

在我的基于网格的游戏中,玩家点击一个单位然后移动他的手指来确定该单位应该移动到哪里。我正在使用 "Jumper" library 进行寻路。获取路径的代码完美运行,但突出显示路径的代码却不尽如人意。

local function onTileTouch( event )
    local phase = event.phase
    local tile = event.target


    if ( phase == "began"  ) then
        -- I could create the line here
    elseif ( phase == "moved" ) then
        createPath( tile )

        -- Getting the position of the first tile based on where the unit is
        local t = tiles[currentSelectedUnit.pos.y][currentSelectedUnit.pos.x]

        -- Create the line at the first tile's position
        line = display.newLine( t.x, t.y, t.x, t.y )
        line:setStrokeColor( 1,0,0 )
        line.strokeWidth = 8

        -- "foundPath" is a table of tiles of the correct path
        for i=1,#foundPath do
            line:append( foundPath[i].x,foundPath[i].y )
        end
    elseif ( phase == "ended" or phase == "cancelled" ) then
end

在 "moved" 阶段创建的行看起来不正确。但是,在 "began" 阶段创建然后在 "moved" 阶段追加时,它确实看起来非常准确。但在这种情况下,会绘制另一条不遵循路径但直接从起始图块到结束图块的额外线。 "began" 阶段方法的第二个问题是我不知道如何继续删除旧行并为新的正确路径创建一个新行。

如果需要任何额外信息,请告诉我。

我不确定你到底想要什么,所以我准备了我自己的版本:

test.lua

local composer   = require( 'composer' )
-- Library setup
local Grid       = require ( 'jumper.grid' ) -- The grid class
local Pathfinder = require ( 'jumper.pathfinder' ) -- The pathfinder lass

local scene = composer.newScene()

-- First, set a collision map and value for walkable tiles
local board  = { 
    tiles = {}, 
    walkable = 0, 
    map = {
        {0,1,0,0,0},
        {0,1,0,1,0},
        {0,1,0,0,0},
        {0,0,0,0,0},
    } 
}

local function createPath( startx, starty, endx, endy )

    -- Creates a grid object
    local grid = Grid( board.map ) 
    -- Creates a pathfinder object using Jump Point Search
    local myFinder = Pathfinder( grid, 'JPS', board.walkable ) 
    -- Calculates the path, and its length
    local path = myFinder:getPath( startx, starty, endx, endy )

    return path

end

function scene:create( event )

    local sceneGroup = self.view
    local rowNum = #board.map
    local colNum = #board.map[1]
    local width  = 40
    local height = width
    local margin = 5

    local function touch( self, event )

        local phase = event.phase
        local endTile = event.target

        if ( phase == 'began'  ) then

            board.startTile = endTile

        elseif ( phase == 'moved' ) then

            if board.startTile ~= endTile then

                if ( board.endTile ~= endTile ) then

                    board.endTile = endTile

                    local path = createPath( board.startTile.col, board.startTile.row, board.endTile.col, board.endTile.row )

                    if path then

                        -- Remove old lines
                        display.remove( board.lines )
                        board.lines = nil           

                        -- Create new line
                        board.lines = display.newLine( sceneGroup, board.startTile.x, board.startTile.y, board.startTile.x, board.startTile.y + 1 )

                        for node, count in path:nodes() do

                            local id = colNum * ( node:getY() - 1 ) + node:getX()
                            local tile = board.tiles[id]
                            board.lines:append( tile.x, tile.y )    

                        end 

                    end 

                end 

            else

                -- Remove old lines
                display.remove( board.lines )
                board.lines = nil
                board.endTile = nil

            end 

        elseif ( phase == 'ended' or phase == 'cancelled' ) then

            -- Remove old lines
            display.remove( board.lines )
            board.lines = nil
            board.endTile = nil
            board.startTile = nil

        end 

    end 

    for i=1, rowNum do

        for j=1, colNum do

            local tile = display.newRect( sceneGroup, (width + margin ) * j, ( height + margin ) * i, width, height ) 
            tile.col = j 
            tile.row = i 
            tile.touch = touch
            -- Add listener and change color for walkable tile
            if board.map[i][j] == board.walkable then

                tile:addEventListener( 'touch' )
                tile:setFillColor( 0, 0.5, 0.5 )

            end 

            local id = colNum * ( i -1 ) + j
            board.tiles[id] = tile

        end

    end     

end

function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase
    if phase == 'will' then

    elseif phase == 'did' then

    end
end

function scene:hide( event )
    local sceneGroup = self.view
    local phase = event.phase
    if event.phase == 'will' then

    elseif phase == 'did' then

    end
end

function scene:destroy( event )
local sceneGroup = self.view


end

scene:addEventListener( 'create', scene )
scene:addEventListener( 'show', scene )
scene:addEventListener( 'hide', scene )
scene:addEventListener( 'destroy', scene )

return scene