--[[ Tutorial by Caleb Place on line drawing, also gives good usage of math.abs. For those of you who want to know, here's how it works: It stores two variables, bx and by, as 0, 0. When you touch the screen (or click), it changes the variables to the event.x and event.y. Now bx=event.x and by=event.y. Then, when you move your touch (or click), it draws a line from the bx and by to the event.x and event.y. Then it resets bx and by to the current position, and starts all over again. That's the way you draw a line! Now, of course, moving the touch (or click) too fast makes it a bit choppy, so you could add some algorithm to smooth it, but that's the basic way to draw it. Please Note: If you go too far to the sides or top with an alpha effect, it can pass over the limit, and you'll get an error saying something like this: WARNING: Attempt to set object.alpha to 1.471 which is outside valid range. It will be clamped to the range [0,1] Just don't pay attention. That's because I did not safeguard against that. Simply un-comment or comment the effects for color, width, alpha, and removal and test it out! quinc --]] display.setStatusBar( display.HiddenStatusBar ) -- Hide the status bar local bx, by=0, 0 -- Create our variables for drawing the line local lines={} local p=1 local tanShade=1000 -- For the camouflage line effect --For the reference: --math.abs returns a positive value of whatever you're trying to get. So it's perfect --for this, as who's ever heard of a line with a width of -3? local function drawALine(event) -- Main function if "began"==event.phase then bx, by=event.x, event.y -- Store the values, if you don't it starts from 0, 0 elseif "moved"==event.phase then lines[p]=display.newLine(bx, by, event.x, event.y) -- Make the line --Width lines[p].width=math.random(1, 30) -- Makes for an interesting "backbone" effect --lines[p].width=12 -- Just a boring old set width --lines[p].width=-3 -- I've heard of a line with a width of -3! --lines[p].width=math.abs(event.y/20) -- 3D-ish horizon look --Alpha --lines[p].alpha=1 -- Another boring one --lines[p].alpha=0.5 -- Half strength line lines[p].alpha=math.abs((event.y-(1000-event.y))/1000) -- Darker area near the center --lines[p].alpha=math.abs(event.y/1000) -- Foggy day --lines[p].alpha=math.abs(event.x/1000) -- Foggy day...to the side? --lines[p].alpha=(math.random(100, 1000))/1000 -- Random --Color --Flat color --lines[p]:setColor(255,255,255) -- White --lines[p]:setColor(255,255,0) -- Yellow --lines[p]:setColor(255,0,0) -- Red --lines[p]:setColor(0,255,0) -- Green --lines[p]:setColor(0,0,255) -- Blue --lines[p]:setColor(255,0,255) -- Purple --Interesting color effects --lines[p]:setColor(math.random(255)) -- Grayscale lines[p]:setColor(math.random(255), math.random(255), math.random(255)) -- Rainbow --lines[p]:setColor(event.x/5, event.x-20/5, event.x+20/5) -- Really odd effect... --lines[p]:setColor(event.x-event.y, 0, event.x-event.y) -- Pretty cool, works best with slow movement --lines[p]:setColor(tanShade/5, tanShade/10, tanShade/15); tanShade=math.random(500, 1500) -- Camouflage --Weird optional effect for i=1, p do lines[i].xScale=lines[i].xScale-0.12; lines[i].yScale=lines[i].yScale-0.12 end bx, by=event.x, event.y -- Reset the bx and by, comment out for a "starburst" effect p=p+1 end end local function clearLines(event) -- Called with a double tap if event.numTaps==2 then --Technique #1: Instant removal --[[ for i=1, p-1 do lines[i]:removeSelf() lines[i]=nil end p=1 --]] --Technique #2: Fade out --[[ for i=1, p-1 do transition.to(lines[i], {alpha=0, time=200, onComplete=function() lines[i]:removeSelf() lines[i]=nil end}) end p=1 --]] --Technique #3: Draw backwards - my favorite removal effect ---[[ local removedLine=1 local function removeNextLine() display.remove(lines[removedLine]) lines[removedLine]=nil removedLine=removedLine+1 end timer.performWithDelay(1, removeNextLine, #lines) p=1 --]] end end --Add both listeners Runtime:addEventListener("tap", clearLines) Runtime:addEventListener("touch", drawALine)