Drag Path help!

Theres something that goes very wrong in the drag path example I have here… It works fine when I start a path from the ball and release my mouse on the goal… If I release my mouse any where else on the screen and not on the word goal it gives me a whole bunch of errors. Does any one know way to tell the program to not draw a line if the mouse has not been released on the goal? Also Does any one know how to make the line show up after you have made a complete path from the ball to goal? That way the player knows that you can only draw a path if it has been connected to the word goal… Martian Controls shows this beautifully… I’m trying to master it but having some trouble… :frowning:
[lua]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)

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)[/lua]

THANKS! [import]uid: 51459 topic_id: 15323 reply_id: 315323[/import]

Hello Jake, I saw you post some questions about paths. So I was wondering if you could help me with something. I’ve been watching the Code of Martian Control Template here http://developer.anscamobile.com/code/martian-control and I want to try this: The ships land in the planets without drawing a path, I mean, each planet be the goal of the ships but I want the ships move to the destination not only linearly, and that you have to touch them to destroy before land. But I dont know how to do that. Could you guide me about it please?

Thank you [import]uid: 81363 topic_id: 15323 reply_id: 64876[/import]

I started looking at Beebe’s code (martian controls) back in april and soon realized that I have to start compiling some code on my own in order for my game to really work the way I want it to…

I finally was able to make an object move from drawing a path from point a to point b…

Then I took that code and broke it down and figured out that the path I was creating was creating to many points that made my rocket ship shaky when I tried to make it follow the path.
So I then had to delete that path and create a smoother path that replaced it which skips points and now my object “Rocket” moves smoothly around the path…

Did you try putting any code together… Like spawning some things…

[lua]local bombs = {}
local bombGroup = display.newGroup()

local function spawnBomb()
local bomb = display.newImage(“bomb.png”)
bomb.x = math.random(_W/2, _W/2)
bomb.name = “bomb”
physics.addBody(bomb, {bounce=0})
bomb.bodyType = “kinematic”
bomb.isSensor = true
bomb.y = math.random(-100, -10)
bomb.index = #bombs + 1
bombGroup:insert(bomb)

bomb:setLinearVelocity(0, math.random(100, 110))
function onCollision(e)
if e.phase == “began” then
bomb:removeSelf()
bomb = nil
end
end
bomb:addEventListener(“collision”, onCollision)

bombs[bomb.index] = bomb

return bomb

end
tmrSpawnBomb = timer.performWithDelay(1000, spawnBomb, 0)

local function onEnterFrame(event)
if bombs ~= nil then
for i, v in pairs(bombs) do
–if bomb leaves right side of screen
if bombs[i].y + bombs[i].width / 2 > display.contentWidth then

print(“bombs removed”)
bombs[i]:removeSelf()
table.remove(bombs, i)
bombs[i] = nil

end
end
end
end

Runtime:addEventListener(“enterFrame”,onEnterFrame)[/lua]

This is an example code that should spawn some bombs in a straight line and remove them… Then try adding a touch event that destroys your ships…

Does any of this make sense? [import]uid: 51459 topic_id: 15323 reply_id: 64877[/import]

also you might want to try this if you want the “ships” to go it a specific spot and maybe fade out…
[lua]local square = display.newRect( 0, 0, 100, 100 )
square:setFillColor( 255,255,255 )

local w,h = display.contentWidth, display.contentHeight

– move square to bottom right corner; subtract half side-length b/c
– the local origin is at the square’s center; fade out square
transition.to( square, { time=1500, alpha=0, x=(w-50), y=(h-50) } )

– fade square back in after 2.5 seconds
transition.to( square, { time=500, delay=2500, alpha=1.0 } )[/lua]

I hope this helps you at least get started :slight_smile: [import]uid: 51459 topic_id: 15323 reply_id: 64881[/import]

Thank you Jake, that’s show me how to start. I was hoping there was some way to store some paths on tables or something in the code of Beebe, but I’ll start something new to see what happens. I appreciate you taking some of your time to answer me.
Regards. [import]uid: 81363 topic_id: 15323 reply_id: 64884[/import]

Oh no problem… Remember when your starting something new like a game idea… Try doing some code test to see how things work and then try other things that connect with your tests and then just maybe you have the start of your game…

Thats how I learned… I had no Idea what I was doing until I did some test which then made me realize how things work in the program… Then I thought of what I can do with those things to make the game fun and challenging …

So now I am at the point where I am really close to a finished game, because I kept at it until I was able to make things work together and do what is known as a game loop… [import]uid: 51459 topic_id: 15323 reply_id: 64890[/import]