Object moves on a defined path example ?

Can anyone provide me a very simple example of how to make an object moves on a defined path (not just straight line but a curve path)? I know the “Flight Path” and “Martian Control” shows how to make an object move along a path…but I am too stupid to understand their code, also seems like they are using two different method to move an object, Flight path sets the X/Y velocity and Martian Control use enterframe to do it, which is the best way to do it??..is there any simple code that shows only an object moves along a defined path ?

Thanks [import]uid: 38556 topic_id: 14114 reply_id: 314114[/import]

There isn’t anything “simple” compared to those two, no.

You aren’t too “stupid” to understand it, you just haven’t had enough experience yet, that’s all :slight_smile: Try to learn a little bit more about Lua and Corona and soon you’ll be able to understand them.

For the record, LOTS of people struggle with that code at first when it’s new to them - so don’t feel bad, please.

Peach :slight_smile: [import]uid: 52491 topic_id: 14114 reply_id: 51976[/import]

elabguy,

Use the code from the following example and build from there. That is what I did!

http://developer.anscamobile.com/forum/2011/07/13/track-concept-need-help

Add this code(line 2) to the example from the link above.

print(path[i].x)  
 print ( "event("..event.x..","..event.y..")")  
 i=i+1  

As you drag your mouse(in the simulator) to define your path, the coordinates will display in the terminal. Use these to create your defined path by creating your own array.

Hope this helps.

J. [import]uid: 20337 topic_id: 14114 reply_id: 52020[/import]

Hey elab,
I know what you mean. I came from a zero programming background ( I mean ZERO, nada, zilch of any kind of any language period) but yet, I still wanted to make games badly.

Then I found Corona, then I got hit with a TON of ideas (I have 43 projects lined out, gameplay elements, screen layouts etc etc etc). The problem is the games I WANT to make I CANNOT make as my level of knowledge just isn’t there. Peach can attest to this, I haunt her techority.com like no ones business, and I ask a ton of questions all the time!

What’s helped me a ton was taking a simple example and adding a small piece to it.

For example, I wanted to build a side scroller with a character who could jump and I wanted the camera to stay centered on the character, unless he got to the end of the level (so it would stop so I wouldn’t see the outside of the map so to speak).

I used the egg breaker demo, found the camera from there and used that for my focus. Then I found another example of using force or linear impulse and used that. Then I found a tilt control example and was able to use that.

So essentially, I was sort of copy and pasting, but really making the code “my own” as I used that as a basis for my game.

Then I wanted to have objects spin, go up and down, elevators etc etc. I found code for that etc.

Then I wanted to add music.

Right now, I am learning collision detection which for me (I know it is for other people as well) is very challenging. This is where the know how really comes in, you need it to play sounds when things hit the ground (like a ball bouncing, or your space ship blowing up). It’s very challenging.
The whole point is I wanted to know about collision detection, and how it worked but I couldn’t get there without knowing all of the other stuff FIRST. I needed to see ok my ball hits object and what happens? What happens if the ball hits nothing (does it time out and remove itself) what if the ball gets stuck how do I handle that? etc etc etc etc.

Start small, grow tall :slight_smile: It takes time. I spend about 3 to 4 hours a day developing and a lot of time reading. Plus, I’m married and I have a baby due sometime within the next 5 days (!!!, first one!!!) so I have to be ULTRA efficient with my time. I drive myself to learn, and you can to just focus on small things and you will notice things will start lining up :slight_smile:
best, ng. [import]uid: 61600 topic_id: 14114 reply_id: 52121[/import]

If you want to connect the ball with the word animate when you drag a path to it…Like in “Martain Controls” use this code -->

[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: 51459 topic_id: 14114 reply_id: 52159[/import]

Thanks Jeff ! it helps !! [import]uid: 38556 topic_id: 14114 reply_id: 52999[/import]

Yes ! that’s exactly how I feels! actually I am an electronic engineer with very weak programing skills. The problem with me is time, I need to work at least 10 hours a day, then 2 hours for traveling … after work I don’t want to look at the computer anymore…feels really tire. I try to spend some time in the weekend on developing my apps, but the progress is really slow.

[import]uid: 38556 topic_id: 14114 reply_id: 53000[/import]

hi Jeff, one silly question, how can I use the print without a new line?? every time you print, corona automatically adds a new line for it, how can you avoid it ?? [import]uid: 38556 topic_id: 14114 reply_id: 53009[/import]

elabguy,

This may not be the most efficient way to convert your points to an array but it should work.

Change the previous code I provided to:

 path[i].y = event.y  
 print ( "x"..event.x.."abc"..event.y.."y")  
 i=i+1  

Drag the mouse to get your points to display in the terminal.

Copy your points, they should look similar to following:
x376abc237y
x375abc236y
x371abc232y
x366abc228y
Paste into BBedit or TextWrangler (Those are the 2 text editors I am familiar with)

Hope this makes sense!

You will now use “Find & Replace All”

  1. Find “\r” Replace with “,”
    Your text should look like this:
    x376abc237y,x375abc236y,etc…

  2. Find “abc” Replace with “,”
    Your text should look like this:
    x376,237y,x375,236y,etc…

  3. To create your points for your x-axis array.
    Make sure “grep” is selected in your search window.
    Find “,…y,x” Replace with “,”
    You now have you x-axis points for your array.
    Copy and Save

Undo the last Find and Replace and change:
Find “,…y,x” Replace with “,”
to
Find “y,x…,” Replace with “,”

You now have your y-axis points for your array.
Copy and Save

You may have to manually clean the first and last point and obviously add your curly brackets.

If you use coordinates that contain a single or 2 digits then just change the grep pattern to “.” or “…”

It may seem like a lot of work but should only take a minute or two.

Hope this helps.

J.
[import]uid: 20337 topic_id: 14114 reply_id: 53656[/import]