Flight Control Game

I want to incourperate the flight control game line drawing, can someone give me a chunk of code that should work to add this feature. It is for a game [import]uid: 39840 topic_id: 12402 reply_id: 312402[/import]

@asiankidspro,
look for Martian Control and the Air Control samples that are available for FREE from Ansca Mobile…

cheers,

?:slight_smile:
Peach, can you make a section on the forums page that has a reference to Source Code, I see that new users do not bother to read, search, they just ask for it and expect it in that thread. So if there was such a section, it would be there for them to see *HOPEFULLY* before they ask for code

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12402 reply_id: 45208[/import]

the following code will give you an idea about what to do

[lua]local path = {}
local i =1;local j =1

local function traceClicks(event)
if event.phase == “moved” then
path[i] = {}
path[i].x = event.x
path[i].y = event.y
print(path[i].x)
i=i+1
end
end

Runtime:addEventListener(“touch”, traceClicks)

–create a circle ball
local ball = display.newCircle( 150, 150, 15 )

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()
posCount = 1
animate = timer.performWithDelay( 10, moveObject, #path )
end

–create a text which when clicked will animate the object
local text = display.newText( “Animate!”, 220, 50, nil, 40 )
text:addEventListener(“touch”, move)[/lua] [import]uid: 71210 topic_id: 12402 reply_id: 45215[/import]

He technowand that was exactly what I was looking for!!! I think its been a month!! Good Looks! [import]uid: 51459 topic_id: 12402 reply_id: 45289[/import]

I ment to say HEY not he last message… Well I did some messing around and was able to import all my own graphics…

I just have one simple question for you technowand…
What does the j & I short for? [import]uid: 51459 topic_id: 12402 reply_id: 45324[/import]

haaa…its just variable declarations… got used to it from the old programming with C days… and in the code that I have given… you don’t need to declare j. its never used. :slight_smile:

[import]uid: 71210 topic_id: 12402 reply_id: 45330[/import]

You should move to the Lua side, and you know that you can declare variables like

local i,j = 1,1

@Technowand, are you still having your setup in Canberra or have you moved to Singapore?

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12402 reply_id: 45333[/import]

HEEEY technowand!! Thanks again… Just got three more questions to get the “ball” rolling…

Is the “ball” suppose to move before you click Animation?
How do I stop it and only play when clicking animation?
Do I need smoothCurve to see the line or is there another way?

[import]uid: 51459 topic_id: 12402 reply_id: 45334[/import]

@jayantv, yes moving slowly to Lua side…just started a few weeks ago… but its not easy to change something which was there in my life for over 15 years… now things are changing …last day I wrote lua code instead of TSQL in SQL server and was puzzled when I got the error “Could not find stored procedure ‘local’.”

Technowand setup at Canberra is still intact. Singapore operations are mainly on mobile applications development side.

we also have full fledged operations in India also. :slight_smile:
[import]uid: 71210 topic_id: 12402 reply_id: 45337[/import]

@jakescarano

that code is not perfect. I just wrote it to explain to you how it works.

the code will keep on adding the touch positions in to the array. but will start the animation from the beginning. small change to the code can fix this.

to start the animation from where you stopped, remove posCount = 1
to restart the retracing on each click. set the array to nil and reset i to 1.

I think this is what you are lookign for

[lua]local path = {}
local i =1;local j =1

local function traceClicks(event)
if event.phase == “moved” then
path[i] = {}
path[i].x = event.x
path[i].y = event.y
print(path[i].x)
i=i+1
end
end

Runtime:addEventListener(“touch”, traceClicks)

–create a circle ball
local ball = display.newCircle( 150, 150, 15 )

local animate
local posCount = 1
–moving the object – this is where animation takes place
local function moveObject(event)
if posCount <= i 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()
animate = timer.performWithDelay( 10, moveObject, #path )
end

–create a text which when clicked will animate the object
local text = display.newText( “Animate!”, 220, 50, nil, 40 )
text:addEventListener(“touch”, move)[/lua]
[import]uid: 71210 topic_id: 12402 reply_id: 45341[/import]

if you want to draw a line when you move the mouse you can absorb idea from the below code.
sorry too busy to combine both together to give a complete solution :slight_smile:

[lua]-- Slash line properties (line that shows up when you move finger across the screen)
local maxPoints = 5
local lineThickness = 5
local endPoints = {}

function main()
local bkg = display.newImage( “backdrop.png” )
Runtime:addEventListener(“touch”, drawLine)
end

function drawLine(event)

– Insert a new point into the front of the array
table.insert(endPoints, 1, {x = event.x, y = event.y, line= nil})

– Remove any excessed points
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, 102, 102, 255 )
line.width = lineThickness
end

if(event.phase == “ended”) then
while(#endPoints > 0) do
table.remove(endPoints)
end
end
end

main()[/lua] [import]uid: 71210 topic_id: 12402 reply_id: 45362[/import]

Thanks technowand Thats exactly what I needed!! [import]uid: 51459 topic_id: 12402 reply_id: 45394[/import]

I can’t thank you enough… Its better that you have them separate so I can understand it better!! [import]uid: 51459 topic_id: 12402 reply_id: 45397[/import]

I was able to connect the line code to the ball and animation button if any body wants to take a look…

[lua]local maxPoints = 2
local lineThickness = 1
local endPoints = {}

local path = {}
local i =1;local j =1

local function drawLine(event)
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

Runtime:addEventListener(“touch”, drawLine)
–Runtime:addEventListener(“touch”, traceClicks)

–create a circle ball
local ball = display.newCircle( 150, 150, 15 )

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()
animate = timer.performWithDelay( 10, moveObject, #path )
end

–create a text which when clicked will animate the object
local text = display.newText( “Animate!”, 220, 50, nil, 40 )
text:addEventListener(“touch”, move)[/lua]

What I need help with now is Connecting point A (ball) to point B (animation) and the ball follows into animation. YOU WIN A MILLION BUCKS!!

thanks

Jake [import]uid: 51459 topic_id: 12402 reply_id: 45569[/import]

there were some minor bugs in the above code so i have rewritten it.
See whether this serves the purpose. If yes, I will send you my bank account number to remit the billion bucks that you offered.
:slight_smile:

[lua]local maxPoints = 2
local lineThickness = 5
local endPoints = {}
local path = {}
local i =1
local traced = 1
local animate
local posCount = 1

–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)

–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)
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( “Animate!”, 220, 50, nil, 40 )
text:addEventListener(“touch”, move)[/lua] [import]uid: 71210 topic_id: 12402 reply_id: 45575[/import]

Hey Techno Wand thanks for all of your help! This is so much easier to understand.

The code seems to be working, but I am getting this error when the ball reaches the word animation

Runtime error
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/285/main.lua:51: attempt to index field ‘?’ (a nil value)
stack traceback:
[C]: ?
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/285/main.lua:51: in function ‘_listener’
?: in function <?:446>
?: in function <?:215>

Is this bad? [import]uid: 51459 topic_id: 12402 reply_id: 45612[/import]

Having an error is of course bad. :slight_smile:

Let me check what the problem is…will post it soon. [import]uid: 71210 topic_id: 12402 reply_id: 45621[/import]

Thanks techno! [import]uid: 51459 topic_id: 12402 reply_id: 45633[/import]

its a small bug
just change posCount <= traced to posCount < traced in the if condition… :))
[import]uid: 71210 topic_id: 12402 reply_id: 45716[/import]

Ok technowand one more thing and I’ll get out of your hair. How do I delete the path as the ball passes through? [import]uid: 51459 topic_id: 12402 reply_id: 45771[/import]