bug using display.newLine?

I am doing a line drawing game, trying to create lines based on the users touch path. The following code is how i assume it should be handled, however during the line drawing process the line is flashing and flickering instead of staying solid. Is this a bug?

[code]
function moveCharacter(event)
if event.phase == “began” and
event.x < (people.x+200) and event.x > (people.x-200) and
event.y < (people.y+200) and event.y > (people.y-200) then
lines={};
for i, v in ipairs(lineTimers) do
if v then
timer.cancel(v);
end
end
lineTimers={};

totalTime=0;
if lineSegments and lineSegments.parent then
lineSegments.parent:remove(lineSegments);
end
lineSegments=nil;
table.insert(lines, {people.x, people.y});

lineSegments = display.newLine(people.x, people.y, event.x, event.y);
lineSegments:setColor(255, 0, 255);
lineSegments.width=3;

elseif event.phase==“ended” then
local intNum=1;
transition.to(lineSegments, {alpha=0, time=3000});
timer.performWithDelay(3000, function()
lineSegments.parent:remove(lineSegments);
lineSegments=nil;
end);
for i, v in ipairs(lines) do
peopleMain.parent:insert(peopleMain);
local lineTimer = timer.performWithDelay(totalTime, function()
–start path traversal
end);
totalTime = totalTime+100;
table.insert(lineTimers, lineTimer);

intNum=intNum+1;
end

else
table.insert(lines, {event.x, event.y});
lineSegments:append(event.x, event.y);

end
end
Runtime:addEventListener(“touch”, moveCharacter);
[/code] [import]uid: 6317 topic_id: 1287 reply_id: 301287[/import]

Well I try to understand your code…

First thing I noticed (but not related most probably) is that your “began” condition contains code to evaluate the area where it begins. But if that condition is false you execute the part I think you want to have executed on “moved”.

Second you remove the object from the group with a timer but allow to have a new one alredy going… which then gets destroyed and your code runs into the void…

But all this is not related to the drawing lines problem…

After that I was curious… and I think I found the problem!

Here my example code for line doodleing

[code]io.output():setvbuf(‘no’)

local lineSegments=nil

local timerCleanup=nil

local lines={}

function moveCharacter(event)
–print(event.phase)

– if a cleanup is sheduled… remove it!
if timerCleanup ~= nil then
timer.cancel(timerCleanup)
timerCleanup=nil
end

if event.phase == “began” then
table.insert(lines, {event.x, event.y});
lines={}; – start new lines list

– if we have a line object left (cleanup not yet done)
if lineSegments then
lineSegments.parent:remove(lineSegments);
end
lineSegments=nil;

table.insert(lines, {event.x, event.y});

– in my example we start at the touch position…
lineSegments = display.newLine(event.x,event.y,event.x,event.y)
lineSegments:setColor(255, 0, 255);
lineSegments.width=3;

elseif event.phase==“ended” then
– i am not sure if the transition.to is ok when the
– object gets removed from the stage while in transition
– but I guess it is… (it should… makes things easier)
transition.to(lineSegments, {alpha=0, time=3000});

– I would maybe use an “enterFrame” handler for fading and removing
– to have full control over what happens… but this here should
– work to…
timerCleanup=timer.performWithDelay(3000, function()
lineSegments.parent:remove(lineSegments);
lineSegments=nil;
end);

– just for the show :slight_smile:
for i, v in ipairs(lines) do
print (v[1], v[2])
end

elseif event.phase==“moved” then
– here we have the “solution” for your problem
– it seems like you have to “reconstruct” the whole
– Vector object… as you CANT add lines after it
– rendered first time!
– This behavious is “documented” I guess… :frowning:
– They say “you can change color and stroke after
– it got rendered”…
table.insert(lines, {event.x, event.y})

– create it all over again…
if lineSegments then
lineSegments.parent:remove(lineSegments)
end

lineSegments = display.newLine(lines[1][1],lines[1][2],
lines[2][1],lines[2][2])
lineSegments:setColor(255, 0, 255)
lineSegments.width=3

– append the remaining part
local i
for i=2,#lines do
lineSegments:append(lines[i][1],lines[i][2])
end
end
return true – we handled the event
end

Runtime:addEventListener(“touch”, moveCharacter);
[/code]

BTW: I had a bit of a performance problem using lines in my game at all… Mostly on the iPhone (less on iPad and iPod) … I think I filed a bug report about it. Don’t know the status…
[import]uid: 6928 topic_id: 1287 reply_id: 3486[/import]

Thanks for the input! I"ll check it out and see if it makes a difference. [import]uid: 6317 topic_id: 1287 reply_id: 3490[/import]

so that appears to have fixed the flickering line issue, however now for some reason the line is always above my other objects on the screen, even if I add the line people.parent:insert(people) right at the ended of the block if event.phase==“ended” the line still appears on top of the actual display objects. so odd [import]uid: 6317 topic_id: 1287 reply_id: 3530[/import]

Create a group for your different “layers” … add them to each other in the right order and “draw” inside those groups instead of the stage. You can help yourself by using rect areas in those groups for their maximum size … you may even use those to filter for the events in the “sensitive” screen area for your path drawing… hope you get what I mean- [import]uid: 6928 topic_id: 1287 reply_id: 3532[/import]

Thanks for the code example, OderWat!

I was trying to write a similar function but the polyLine documentation is a little… sparse. Your code example prevented me from pulling the rest of my hair out.

[import]uid: 9422 topic_id: 1287 reply_id: 9077[/import]