Help with Line path

I have this code working exactly the way I want expect 1 minor detail.. I want the path to delete when ball passes through it.. Can someone please help..

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
local maxPoints = 2
local lineThickness = 5
local endPoints = {}
local path = {}
local i =1
local traced = 1
local animate
local posCount = 1
local messageTween
 
--create a circle ball
local ball = display.newCircle( 150, 150, 15 ) 
 
local function drawLine(event)
  if event.phase == "began" then
  
    for i=1, traced, 1 do
        table.remove (path ,i)
        end
    i = 1
        traced = 1
        
  elseif event.phase == "moved" then
    table.insert(endPoints, 1, {x = event.x, y = event.y, line= nil})
    path[i] = {}
    path[i].x = event.x
    path[i].y = event.y
    i=i+1
        traced = traced + 1
    if(#endPoints > maxPoints) then 
        table.remove(endPoints)
    end
 
    for i,v in ipairs(endPoints) do
        local line = display.newLine(v.x, v.y, event.x, event.y)
        line:setColor( 255, 255, 255, 255 )
        line.width = lineThickness
    end
 
  elseif(event.phase == "ended") then     
                while(#endPoints > 0) do
          table.remove(endPoints)
        end
  end
end
 
Runtime:addEventListener("touch", drawLine)
 
local goalReached = function( xLoc, yLoc )
                -- This function is called when a flying object has successfully arrived at
                -- the correct destination.
                
                        -- The Code Below Shows Floating Score "+1000" (etc) display
                local xLoc = xLoc
                local yLoc = yLoc
                
                local writeTextAbovePlanet = function( textLineOne, theX, theY )
                
                        local theString = textLineOne
                        local theX = theX
                        local theY = theY
                        
                        local screenMessage = display.newText( theString, 160, 240, "080203", 44 )
                        screenMessage:setTextColor( 255, 255, 255, 255 )
                        screenMessage.alpha = 1.0
                        screenMessage.xScale = 0.5; screenMessage.yScale = 0.5
                        screenMessage.xOrigin = theX
                        screenMessage.yOrigin = theY
                        
                        local destroyMessage = function()
                                if screenMessage ~= nil then
                                        screenMessage:removeSelf()
                                        screenMessage = nil
                                end
                        end
                        
                        local newY = theY - 35
                        
                        messageTween = transition.to( screenMessage, { time=1000, alpha=0, y=newY, onComplete=destroyMessage } )
                end
                
                writeTextAbovePlanet( "+1000", xLoc, yLoc )
        end
 
 
 
 
 
--moving the object -- this is where animation takes place
local function moveObject(event)
     if posCount < traced then
        ball.x = path[posCount].x
        ball.y = path[posCount].y
        posCount = posCount + 1
     else
                 if animate then
         timer.cancel(animate)
         goalReached()
         end
     end
end
 function anim() 
  animate = timer.performWithDelay( 10, moveObject, traced ) 
 end
 
 
-- to move ball when we click the animate text
local function move(event)
if(event.phase == "ended") then    
   posCount = 1
   transition.to(ball, { time = 500, x= path[1].x, y =path[1].y, onComplete= anim})
end
return true
end
 
 
 
 
--create a text which when clicked will animate the object
local text = display.newText( "Goal!", 220, 50, nil, 40 )
text.x = 450; text.y = 425
text:addEventListener("touch", move)

Have you tried using .isVisible?

On all the sections of the path that the ball moves over you could set .isVisible to false.

I just tried that and it didn't work! I don't think I have it done right.. Can you take a look notts_forest_

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
local maxPoints = 2
local lineThickness = 5
local endPoints = {}
local path = {}
local i =1
local traced = 1
local animate
local posCount = 1
local messageTween
 
--create a circle ball
local ball = display.newCircle( 150, 150, 15 ) 
 
local function drawLine(event)
  if event.phase == "began" then
  
    for i=1, traced, 1 do
        table.remove (path ,i)
        end
    i = 1
        traced = 1
        
  elseif event.phase == "moved" then
    table.insert(endPoints, 1, {x = event.x, y = event.y, line= nil})
    path[i] = {}
    path[i].x = event.x
    path[i].y = event.y
    i=i+1
        traced = traced + 1
    if(#endPoints > maxPoints) then 
        table.remove(endPoints)
    end
 
    for i,v in ipairs(endPoints) do
        local line = display.newLine(v.x, v.y, event.x, event.y)
        line:setColor( 255, 255, 255, 255 )
        line.width = lineThickness
    end
 
  elseif(event.phase == "ended") then     
                while(#endPoints > 0) do
          table.remove(endPoints)
          path.isVisible = true
        end
  end
end
 
Runtime:addEventListener("touch", drawLine)
 
 
--moving the object -- this is where animation takes place
local function moveObject(event)
     if posCount < traced then
        ball.x = path[posCount].x
        ball.y = path[posCount].y
        posCount = posCount + 1
        path.isVisible = false
     else
                 if animate then
         timer.cancel(animate)
         goalReached()
         end
     end
end
 function anim() 
  animate = timer.performWithDelay( 10, moveObject, traced ) 
 end
 
 
-- to move ball when we click the animate text
local function move(event)
if(event.phase == "ended") then    
   posCount = 1
   transition.to(ball, { time = 500, x= path[1].x, y =path[1].y, onComplete= anim})
end
return true
end
 
 
 
 
--create a text which when clicked will animate the object
local text = display.newText( "Goal!", 220, 50, nil, 40 )
text.x = 450; text.y = 425
text:addEventListener("touch", move)

I think you need path[poscount].isVisible = false

before you do poscount = poscount + 1

Hey Rob! Now the ball doesn't move when I add path[poscount].isVisible = false before poscount = poscount + 1.. :(

Actually that's not going to work and it appears to me that the line's can't be erased.

If you look at where the line drawing happens, its a local variable called line that is scoped inside a for loop, inside the function drawLine.

So as you move your finger around the display (or the mouse in the sim) hundreds of line objects are being created, drawn and then lost. This is a prime example of a memory leak. Each of those line objects exist, but you no longer have a reference to them to remove them.

The path table is just a set of x, y coordinates. So hiding anything from "path" isn't going to display anything since path has no display objects.

The function drawLine is also doing an i=i+1 which in itself is okay, but right below it, there is a for i,v in ipairs() loop that is overwriting i. It looks like this code is trying to use "i" for two different purposes. I'm trying to figure this code out. It appears there was plans to store the line in the endPoints array since on the table insert, they are setting line to nil.

Hey Rob, memory leeks are no good!! :(

What if I have something like this?

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
local ui = require("ui")
 
 
 
local maxPoints = 2
local lineThickness = 1
local endPoints = {}
local path = {}
local i =1;local j =1
local traced = 1
 
local ball = display.newCircle( 150, 150, 15 )
 
local function drawLine(event)
 
        if event.phase == "began" then
  
    for i=1, traced, 1 do
        table.remove (path ,i)
        end
    i = 1
        traced = 1
 
   if event.phase == "moved" then
        
        
 
        table.insert(endPoints, 1, {x = event.x, y = event.y, line= nil})
      path[i] = {}
      path[i].x = event.x
      path[i].y = event.y
          print(path[i].x)
      i=i+1
      if(#endPoints > maxPoints) then 
                table.remove(endPoints)
        end
 
        for i,v in ipairs(endPoints) do
                local line = display.newLine(v.x, v.y, event.x, event.y)
                line:setColor( 255, 255, 255, 255 )
                line.width = lineThickness
        end
 
        if(event.phase == "ended") then         
                while(#endPoints > 0) do
                        table.remove(endPoints)
              
        end
 
   end
end
end
end
 
 
Runtime:addEventListener("touch", drawLine)
--Runtime:addEventListener("touch", traceClicks)
 
 
local animate
local posCount = 1
--moving the object -- this is where animation takes place
local function moveObject(event)
     if posCount <= #path then
        c.x = path[posCount].x
        c.y = path[posCount].y
        posCount = posCount + 1
     else
         timer.cancel(animate)
     end
end
 
-- to move ball when we click the animate text
local function move(event)
 
if(event.phase == "ended") then    
    posCount = 1        
        
        path[1] = {}
      path[1].x = event.x
      path[1].y = event.y
        transition.to(c, { time = 500, x= path[1].x, y =path[1].y, onComplete= anim})
 
end
return true
end
--end
 
text:addEventListener("touch", move)<lua>

leak*

this code doesn't show the lines does that mean no leaks?

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
local ui = require("ui")
 
 
 
local maxPoints = 2
local lineThickness = 1
local endPoints = {}
local path = {}
local i =1;local j =1
local traced = 1
 
local ball = display.newCircle( 150, 150, 15 )
 
local function drawLine(event)
 
        if event.phase == "began" then
  
    for i=1, traced, 1 do
        table.remove (path ,i)
        end
    i = 1
        traced = 1
 
   if event.phase == "moved" then
        
        
 
        table.insert(endPoints, 1, {x = event.x, y = event.y, line= nil})
      path[i] = {}
      path[i].x = event.x
      path[i].y = event.y
          print(path[i].x)
      i=i+1
      if(#endPoints > maxPoints) then 
                table.remove(endPoints)
        end
 
        for i,v in ipairs(endPoints) do
                local line = display.newLine(v.x, v.y, event.x, event.y)
                line:setColor( 255, 255, 255, 255 )
                line.width = lineThickness
        end
 
        if(event.phase == "ended") then         
                while(#endPoints > 0) do
                        table.remove(endPoints)
              
        end
 
   end
end
end
end
 
 
Runtime:addEventListener("touch", drawLine)
 
  
local animate
local posCount = 1
--moving the object -- this is where animation takes place
local function moveObject(event)
     if posCount <= #path then
        ball.x = path[posCount].x
        ball.y = path[posCount].y
        posCount = posCount + 1
     else
         timer.cancel(animate)
     end
end
 
-- to move ball when we click the animate text
local function move(event)
 
if(event.phase == "ended") then    
    posCount = 1        
        
        path[1] = {}
      path[1].x = event.x
      path[1].y = event.y
        transition.to(ball, { time = 500, x= path[1].x, y =path[1].y, onComplete= anim})
 
end
return true
end
--end
local text = display.newText( "Goal!", 220, 50, nil, 40 )
text.x = 450; text.y = 425
text:addEventListener("touch", move)

I got it working. I'll post the code later

Thanks Rob!!

Here ya go!

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
local maxPoints = 2
local lineThickness = 5
local endPoints = {}
local path = {}
local i =1
local traced = 1
local animate
local posCount = 1
local messageTween
 
--create a circle ball
local ball = display.newCircle( 150, 150, 15 ) 
 
local function drawLine(event)
    if event.phase == "began" then
        for i=1, traced, 1 do
            table.remove (path ,i)
        end
        idx = 1
        traced = 1
    elseif event.phase == "moved" then
        table.insert(endPoints, 1, {x = event.x, y = event.y, line= nil})
        path[i] = {}
        path[i].x = event.x
        path[i].y = event.y
        i = i + 1
 
        if(#endPoints > maxPoints) then 
            table.remove(endPoints)
        end
        for i,v in ipairs(endPoints) do
            local line = display.newLine(v.x, v.y, event.x, event.y)
            line:setColor( 255, 255, 255, 255 )
            line.width = lineThickness
            path[traced].line = line
        end
        traced = traced + 1
    elseif(event.phase == "ended") then     
        while(#endPoints > 0) do
            table.remove(endPoints)
            path.isVisible = true
        end
    end
end
 
Runtime:addEventListener("touch", drawLine)
 
 
--moving the object -- this is where animation takes place
local function moveObject(event)
     if posCount < traced then
        ball.x = path[posCount].x
        ball.y = path[posCount].y
        if path[posCount].line then
            path[posCount].line.isVisible = false
            path[posCount].line:removeSelf()
            path[posCount].line = nil
        end
        posCount = posCount + 1
        --path.isVisible = false
     else
        if animate then
            timer.cancel(animate)
            goalReached()
        end
     end
end
 function anim() 
  animate = timer.performWithDelay( 10, moveObject, traced ) 
 end
 
 
-- to move ball when we click the animate text
local function move(event)
if(event.phase == "ended") then    
   posCount = 1
   transition.to(ball, { time = 500, x= path[1].x, y =path[1].y, onComplete= anim})
end
return true
end
 
 
 
 
--create a text which when clicked will animate the object
local text = display.newText( "Goal!", 100, 50, nil, 40 )
text.x = 100; text.y = 425
text:addEventListener("touch", move)

Thank you so much much for helping me out you are so AWESOME! I owe you big time Rob!!

Hey Rob! Just ran a test this morning and it gave me this error

Runtime error
...ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/324/main.lua:64: attempt to call global 'goalReached' (a nil value)
stack traceback:
[C]: in function 'goalReached'
...ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/324/main.lua:64: in function '_listener'
?: in function <?:446>
?: in function <?:215>

Should I be worried about this?

p.s Hope you are somewhere safe for the hurricane.. :)

Yes Jake you need to worry about it.

Your program is trying to call a function "goalReached" which doesn't exist. I assumed that you either didn't include it or haven't written it yet. But you need to do something when the goal is reached.

What do you want it to do?

I will be removing the rocket and adding points of course.. But thats when I add it to the game. I thought it had to do with that. Cool well thanks for all your help Rob!! Your AWESOME!!

I GOT A GREAT SKELETON TO WORK FROM NOW!! I OWE YOU BIG TIME!!

views:1804 update:2011/10/4 17:12:07
corona forums © 2003-2011