Line drawing dashed line

Hi Folks,

I'am pretty busy at the moment on developing a APP of the month ;) and have a lot of stuff working so far.

Only thing i wan't to implement is the following, now it works on single touches.

I need the user to draw a line from one object, and after that the object follows the drawn path. Not so hard, there are some samples and even a full project with examples, cheers and thanks for that.

The drawing go's oke, but the only issue i'am fighting is the following:

When the user draws a line, i like to have a dashed line, like in flight control.

Example

-- -- -- -- -- -- -- -- --

But then also curved offcourse :)

Could anyone probably help me on this one.

Cheers,
Jespar

Hi.

I modified a little bit the line drawing code from gtatarkin.

The code didn't draw lines with the same length. Perhaps it helps to find a good solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
-- original source code: Gesture recognition library for Corona SDK
-- http://developer.anscamobile.com/code/gesture-recognition-library-corona-sdk
 
-- modified 110324 by sidasa
 
 
local moved = 0
 
local pointsTable = {}
local line
 
 
local function drawLine ()
 
        if line then
                line:removeSelf()
        end
        
        local numPoints = #pointsTable
        local nl = {}
        local  j, p
        
        local linewidth = 5
                 
        nl[1] = pointsTable[1]
                 
        j = 2
        p = 1
                 
        for  i = 2, numPoints, 1  do
                nl[j] = pointsTable[i]
                j = j+1
                p = i 
        end
        
        if ( p  < numPoints -1 ) then
                nl[j] = pointsTable[numPoints-1]
        end
        
        if #nl > 2 then
                        line = display.newLine(nl[1].x,nl[1].y,nl[2].x,nl[2].y)
                        line.width = linewidth
                        line:setColor(0,0,0)
                        
                        for i = 3, #nl, 1 do 
                                -- draw next "step"
                                if #nl > i then
                                        line = display.newLine(nl[i].x,nl[i].y,nl[i+1].x,nl[i+1].y)
                                else
                                        line:append( nl[i].x,nl[i].y);
                                end
                                line.width = linewidth
                                
                                -- change color every "step"
                                if i%2 == 0 then
                                        line:setColor(0,0,0)
                                else
                                        line:setColor(255,255,255)
                                end
                                
                        end
        end
end
 
local function Update(event)            
        if "began" == event.phase then
                pointsTable = nil
                pointsTable = {}
                local pt = {}
                pt.x = event.x
                pt.y = event.y
                table.insert(pointsTable,pt)
        
        elseif "moved" == event.phase then
        
                local pt = {}
                pt.x = event.x
                pt.y = event.y
                table.insert(pointsTable,pt)
                moved = 1 --
        
        elseif "ended" == event.phase or "cancelled" == event.phase then
                        if( moved == 1 ) then
                                drawLine ()
                        moved = 0
                        end
        end
end
 
Runtime:addEventListener( "touch", Update )

Thanks sidasa,

I will check it out if it will be usable and fast enough ;)

Cheers,
Jespar

Hey guys:

I am new to Lua and Corona SDK. I was wondering how would I modify the above code so that it does solid lines instead of dashes? Also, how can I have a object9(s) following the path of the draw line(s).

Thank you so much!

Willy J.

Actually I figured that out!...

But, I have another question though. How can I get it to paint as I touch across the screen?

Thanks,

Willy J.

Okay, I figured this one out as well!

Willy J.

views:2812 update:2011/9/29 19:21:19
corona forums © 2003-2011