I’m still having an issue with smooth drawing.
I have put the line that draws into a enterFrame event, but still it’s leaving gaps.
Help!!
Here’s the code!
local widget = require “widget”
– This is only included because I used the widget.newButton API for the example “undo” & “erase” buttons. It’s not required for the drawing functionality.
local moveTool = {}
drawScratchLine = false
– VARIABLES & LINE TABLE (required)
local lineTable = {}
– This is a required table that will contain each drawn line as a separate display group, for easy referral and removal
– create object
local myObject = display.newImage(“ScorchPen200x81.png”,0,0,100,100)
local function scratchLine(event)
if drawScratchLine == true then
local line = display.newImage(“FeatherTip_4x2.png”,linePoints[#linePoints-1].x,linePoints[#linePoints-1].y,linePoints[#linePoints].x,linePoints[#linePoints].y)
lineTable[i]:insert(line)
drawScratchLine = false
end
end
function myObject:touch( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
i = #lineTable+1
lineTable[i]=display.newGroup()
linePoints = nil
linePoints = {};
local pt = {}
–pt.x = event.x;
–pt.y = event.y;
pt.x = t.x0
pt.y = t.y0
table.insert(linePoints,pt);
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
local pt = {}
pt.x = t.x - 100;
pt.y = t.y + 30;
if not (pt.x==linePoints[#linePoints].x and pt.y==linePoints[#linePoints].y) then
table.insert(linePoints,pt)
–lineTable[i]:insert(line)
drawScratchLine = true
end
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
i=nil
–t:pause()
end
end
– Stop further propagation of touch event!
return true
end
– UNDO & ERASE FUNCTIONS (not required)
local undo = function()
if #lineTable>0 then
lineTable[#lineTable]:removeSelf()
lineTable[#lineTable]=nil
end
return true
end
local erase = function()
for i = 1, #lineTable do
lineTable[i]:removeSelf()
lineTable[i] = nil
end
return true
end
– UNDO & ERASE BUTTONS (not required)
local undoButton = widget.newButton{
left = 25,
top = display.contentHeight - 50,
label = “Undo”,
width = 100, height = 28,
cornerRadius = 8,
onRelease = undo
}
local eraseButton = widget.newButton{
left = display.contentWidth-125,
top = display.contentHeight - 50,
label = “Erase”,
width = 100, height = 28,
cornerRadius = 8,
onRelease = erase
}
–function MyClass:enterFrame( event )
– print( "enterFrame called at time: " … event.time )
–end
–function moveTool:touch(event)
–self.markX = self.x – store x location of object
–self.markY = self.y – store y location of object
–local mapX, mapY = map:getPosition()
– local diffX = event.x ;
– local diffY = event.y ;
–myObject.transition = transition.to( myObject, { time = 500, x =diffX ,y = diffY, onComplete = onTransitionComplete } )
–transition.to( myObject, {time=500, delay=3500, alpha=0})
–transition.to( myObject, { time = 500, x =diffX ,y = diffY, onComplete = onTransitionComplete } )
local function moveTool(event)
local diffX = event.x;
local diffY = event.y;
–transition.to(myObject, {time=500, delay=0, alpha=0})
transition.to( myObject, { time = 5, x =diffX ,y = diffY, onComplete = onTransitionComplete } )
end
Runtime:addEventListener( “enterFrame”, scratchLine )
myObject:addEventListener( “touch”, myObject )
Runtime:addEventListener(“touch”, moveTool) [import]uid: 44287 topic_id: 22634 reply_id: 322634[/import]