Sorry, SGS, while I totally respect what you do here on the forums and the code you write, I also have to disagree this time. As that is the case, I guess the burden is on me seeing as you’ve provided smaller code.
1. A “moved” event will only fire it the touch point has actually moved from the previous touch point
This is true, though the granularity of the points could be almost on top of each other. It is definitely possible to produce touch event values where there two consecutive move phase locations are less than 1 pixel or point apart. This, in my estimation, is causing the ‘folding’ (for want of a better description) in the original code. It does not happen in my code, though I see it happening in your code as well. I’m not sure why it’s not evident in your screenshot, but copy/pasting your code into a brand new main.lua and running it cold produced the same result as Rene found.
3. Destroying the line on each “moved” update and redrawing it as that is massively inefficient. It is like reading a page of text and for each new word you read you start at the beginning of the page again!
I do agree here. I was posting code which went quite a bit further than really necessary, I’ll admit. For example, the use of the .hasFocus value is to handle situations where the touch event moved over another object which could handle touch events and was not sensibly filtering input. In that case, you could easily find the touch event being picked up by another touch handler and everything would go wrong. Therefore, I always include that logic, even in the smallest examples.
Having said that, the reason to delete the line and recreate it again is because I’m used to thinking that way because in my typical situation it is logical. Without needlessly going into detail, I will say that the number of points used to render various things should be controlled or things will eventually go wrong.
I will say that the code could be improved not to delete the line but the problem of one touch being too close to the previous still needs to be handled. I would comment, though, that any decent device these days should be able to delete a line and recreate it without issue. I certainly wouldn’t do it with anything more complex.
FYI, here is a simplified version for you (so you can learn). Don’t ever have 20+ lines of code when 10 does the job!
Of course, that’s a great starting point and I kinda wish I’d provided simpler code now. Having said that, here’s a version of my code which does not delete the line. It doesn’t really reduce the number of code lines but it won’t have the performance issue and the points used to build the line will not be excessive (One flaw is that it is no longer possible to retrieve the list of x,y values which build the line, which I consider a requirement - even for learning):
local group, pt, line = display.newGroup(), {}, nil local function length( ax, ay, bx, by ) local width, height = bx-ax, by-ay return (width\*width + height\*height)^0.5 end function group:touch(event) if (event.phase == "began") then group.hasFocus = true display.currentStage:setFocus( group ) pt = { x=event.x, y=event.y } return true elseif (group.hasFocus) then if (event.phase == "moved") then if (length( pt.x, pt.y, event.x, event.y ) \> 1) then pt = { x=event.x, y=event.y } if (line == nil) then line = display.newLine( event.xStart, event.yStart, pt.x, pt.y ) line:setStrokeColor (1,1,1) line.strokeWidth = 20 else line:append( pt.x, pt.y ) end end else group.hasFocus = false display.currentStage:setFocus( nil ) end return true end return false end Runtime:addEventListener( "touch", group )