Moving with pause

I have the following situation that i couldn't move on yet. Hope somebody can bring some clear ideas.

I need to move an object from point A to D, stopping in points B, C first. So, I have a table with all end points (x and y) and I am looping the .x and .y positions. The issue is I cannot control the duration of the movement. I would like to say, from point A to D, it will take 5 seconds.

Anyways, this is my current 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
xisis = { 610,503,621,601,493,454,610,503,621,601,493,454 }
yisis = { 891,687,637,432,356,521,891,687,637,432,356,521 }
 
bee = display.newImageRect( "bee.png", 26, 28 ); 
bee.x = 610; bee.y = 891; bee.alpha = 1 
 
local sec = 5 --5 seconds or the animation duration
local msec = sec / (#xcoords - 0) --duration divided by the number of points
 
local coordCount = 1
local function moveObject()
        if coordCount <= #xcoords then
 
                bee.x = xisis[ coordCount ]
                bee.y = yisis[ coordCount ]
                coordCount = coordCount + 1
 
                --I tried a timer to wait "msec" but it didn't pause the loop
                --I tried a gtween transition with the same results of the timer
       else
                Runtime:removeEventListener( "enterFrame", moveObject )
        end
end
 
 
Runtime:addEventListener( "enterFrame", moveObject )

You could try calling moveObject() from a timer.

ie

1
2
3
4
5
local function beginBeeMove()
coordCount = 1
moveBee = timer.performWithDelay( 417, moveObject, #xcoords )
--the 417 is the equiv of 5 seconds along 12 points
end

Thanks J! Unfortunately it is not working.

First time running the bee keeps jumping between points (back and forth), probably while the 417 milliseconds are not done yet.

Restarting the animation, I get en error on the timer.cancel line:
attempt to index global 'event' (a nil value).

Ideas?

I tried creating something similar to your code. Albeit the timing is a bit wonky, I changed the 417 to 800, but should give you an idea about what may work. I just subtracted 400 from each of your coordinates to fit on my screen. Try this standalone.

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
local totalCoords = 12
local posCount = 1
local xisis = {}
local yisis = {}
 
background = display.newImage( "whiteBG.png" )
display.setStatusBar( display.HiddenStatusBar )
 
xisis = { 210,103,221,201,93,54,210,103,221,201,93,54 }
yisis = { 491,287,237,32,56,121,491,287,237,32,56,121 }
 
 
local bee = display.newRect( 150, 150, 50, 50 ); 
bee:setFillColor(60,225,200,150)
bee.alpha = 1 
 
local coordCount = 1
local function moveBee()
        if posCount <= totalCoords then
        print(posCount)
        print(totalCoords)
        print(coordCount)
        bee.x = xisis[coordCount]
        bee.y = yisis[coordCount]
        coordCount = coordCount + 1
        posCount = coordCount
        else
         timer.cancel(moveAlongCoords)
         Runtime:removeEventListener( "enterFrame", beginBeeMove )
        end
end
--Runtime:addEventListener( "enterFrame", beginBeeMove )
 
local function beginBeeMove()
moveAlongCoords = timer.performWithDelay( 800, moveBee, totalCoords )
end
 
local text = display.newText( "Test Bee Move", 50, 250, nil, 25 )
text:setTextColor(0,127,127)
text:addEventListener("touch", beginBeeMove)

The timer was off because the touch event I have fires twice. Once during the touch and again during release(duh). Change to a "tap" event and all is good. The 417 is correct for your "enterFrame" listener from my original post.

J.

Thanks a lot J. I am working also with a gtween version (having split the path in small segments), mostly because I will need to control time and speed between each point.

views:1511 update:2011/9/27 8:54:05
corona forums © 2003-2011