In my grid-based game, The player clicks on a unit then he moves his finger to determine where this unit should move. I’m using the “Jumper” library for the pathfinding. The code for getting the path works perfectly, but the code for highlighting the path, not so much.
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
The line doesn’t look right when being created in the “moved” phase. It does, however, look very accurate when being created in the “began” phase and then getting appended during the “moved” phase. But in this case, another extra line gets drawn that doesn’t follow the path but gets directly from the start tile to the end tile.
My second problem with the “began” phase method, is I don’t know how to keep deleting the old line and create a new one with for the new correct path.
Let me know if any extra information is needed.
PS: The code editor in this forums is extremely buggy, it keeps randomly jumping to the bottom line.