Highlight a path in a grid

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.

I’ve updated the OP with a new code.

Are you you using Rolands original jumper or Panc’smodified version?

I’d take a look at the modified version and code in the root folder of the repository.

Does anyone have any idea how to do this? I honestly don’t know why the line is not behaving as it should. 

What I want is in a touch “moved” phase draw a line based on coordinates from a table. and each time the “moved” phase gets triggered, delete the old one and draw a new one. 

You’re asking folks to solve this out of context. This is the kind of thing you’d need to see all the code for.

Also, I think very few people who answer questions here use Jumper.

Note: This is the kind of thing I’d normally suggest a hit for.  You’re unlikely to get help otherwise.  The pool of possible helpers who also have experience with Jumper and can grok what you’re doing is too small.

“Jumper” has nothing to do with the problem. I just mentioned it to give “context” of what I’m doing, The problem is as I stated in my last post above.
In any case, I’ve managed to solve the problem, it was very stupid and I guess it’s some sort of Corona limitation or something. I changed the code that creates the line to this:

line = display.newLine( t.x, t.y, t.x+0.1, t.y+0.1 )

Ah, my apologies.  Yes, that is incorrect usage:

 line = display.newLine( t.x, t.y, t.x, t.y )

You can’t have a line with no length.

Also, you want to be careful about drawing segments with a length of nearly zero as that may also fail.

Now that I know this isn’t about jumper I’ve taken a closer look and this is my suggestion:

local line local function onTileTouch( event ) local phase = event.phase local tile = event.target if ( phase == "moved" ) then createPath( tile ) display.remove(line) -- cleaning up old assumed to exist line. if ( #foundPath \> 1 ) then local t = tiles[currentSelectedUnit.pos.y][currentSelectedUnit.pos.x] line = display.newLine( t.x, t.y, foundPath[1].x,foundPath[1].y ) line:setStrokeColor( 1,0,0 ) line.strokeWidth = 8 for i = 2, #foundPath do line:append( foundPath[i].x,foundPath[i].y ) end end end 

Or, if t is the first point on the path, then

local line local function onTileTouch( event ) local phase = event.phase local tile = event.target if ( phase == "moved" ) then createPath( tile ) display.remove(line) -- cleaning up old assumed to exist line. if ( #foundPath \> 1 ) then local t = tiles[currentSelectedUnit.pos.y][currentSelectedUnit.pos.x] line = display.newLine( t.x, t.y, foundPath[2].x,foundPath[2].y ) line:setStrokeColor( 1,0,0 ) line.strokeWidth = 8 for i = 3, #foundPath do line:append( foundPath[i].x,foundPath[i].y ) end end end

Thanks for the help.

My original idea was at that point of time I don’t know in which direction the line should move, so I just create it at the original position.

The display.newLine() page has a “gotcha” section but it doesn’t mention this limitation. 

re: Gotchas - I guess they can’t think of everything.  I mean, why would you make a zero length line? :)  That is a devolved case.

I’ve updated the OP with a new code.

Are you you using Rolands original jumper or Panc’smodified version?

I’d take a look at the modified version and code in the root folder of the repository.

Does anyone have any idea how to do this? I honestly don’t know why the line is not behaving as it should. 

What I want is in a touch “moved” phase draw a line based on coordinates from a table. and each time the “moved” phase gets triggered, delete the old one and draw a new one. 

You’re asking folks to solve this out of context. This is the kind of thing you’d need to see all the code for.

Also, I think very few people who answer questions here use Jumper.

Note: This is the kind of thing I’d normally suggest a hit for.  You’re unlikely to get help otherwise.  The pool of possible helpers who also have experience with Jumper and can grok what you’re doing is too small.

“Jumper” has nothing to do with the problem. I just mentioned it to give “context” of what I’m doing, The problem is as I stated in my last post above.
In any case, I’ve managed to solve the problem, it was very stupid and I guess it’s some sort of Corona limitation or something. I changed the code that creates the line to this:

line = display.newLine( t.x, t.y, t.x+0.1, t.y+0.1 )

Ah, my apologies.  Yes, that is incorrect usage:

 line = display.newLine( t.x, t.y, t.x, t.y )

You can’t have a line with no length.

Also, you want to be careful about drawing segments with a length of nearly zero as that may also fail.

Now that I know this isn’t about jumper I’ve taken a closer look and this is my suggestion:

local line local function onTileTouch( event ) local phase = event.phase local tile = event.target if ( phase == "moved" ) then createPath( tile ) display.remove(line) -- cleaning up old assumed to exist line. if ( #foundPath \> 1 ) then local t = tiles[currentSelectedUnit.pos.y][currentSelectedUnit.pos.x] line = display.newLine( t.x, t.y, foundPath[1].x,foundPath[1].y ) line:setStrokeColor( 1,0,0 ) line.strokeWidth = 8 for i = 2, #foundPath do line:append( foundPath[i].x,foundPath[i].y ) end end end 

Or, if t is the first point on the path, then

local line local function onTileTouch( event ) local phase = event.phase local tile = event.target if ( phase == "moved" ) then createPath( tile ) display.remove(line) -- cleaning up old assumed to exist line. if ( #foundPath \> 1 ) then local t = tiles[currentSelectedUnit.pos.y][currentSelectedUnit.pos.x] line = display.newLine( t.x, t.y, foundPath[2].x,foundPath[2].y ) line:setStrokeColor( 1,0,0 ) line.strokeWidth = 8 for i = 3, #foundPath do line:append( foundPath[i].x,foundPath[i].y ) end end end

Thanks for the help.

My original idea was at that point of time I don’t know in which direction the line should move, so I just create it at the original position.

The display.newLine() page has a “gotcha” section but it doesn’t mention this limitation. 

re: Gotchas - I guess they can’t think of everything.  I mean, why would you make a zero length line? :)  That is a devolved case.