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:

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 )  

As you can see in the comments:
–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

Thanks!
[import]uid: 4883 topic_id: 13752 reply_id: 313752[/import]

You could try calling moveObject() from a timer.

ie

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

and change the following to:

else  
 timer.cancel(event.source)  
 Runtime:removeEventListener( "enterFrame", beginBeeMove )  
  
 end  
end  
   
   
Runtime:addEventListener( "enterFrame", beginBeeMove )  
  

If you want to stop at various points along the way just cancel the timer. To resume just start call your timer from the updated coordCount. If you want to pause at certain points you can change the 417 into a variable and after coordCount add something like below.

coordCount = coordCount + 1  
if #xcoords == 4 then  
 417 variable = 417 + amount of pause time --Then pass to timer  
else  
do nothing  
end  

Hope this helps.

j [import]uid: 20337 topic_id: 13752 reply_id: 50524[/import]

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? [import]uid: 4883 topic_id: 13752 reply_id: 50597[/import]

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.

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)  

Cheers!

J. [import]uid: 20337 topic_id: 13752 reply_id: 50614[/import]

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. [import]uid: 20337 topic_id: 13752 reply_id: 50724[/import]

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. [import]uid: 4883 topic_id: 13752 reply_id: 50760[/import]