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

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

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.

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

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

best, ng.

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

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

Thanks Jeff ! it helps !!

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.

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 ??

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:

1
2
3
  path[i].y = event.y
          print ( "x"..event.x.."abc"..event.y.."y")
      i=i+1
views:1978 update:2011/10/4 17:12:07
corona forums © 2003-2011