Moving Objects (comparing 2 codes)

Why this work:

local plane = display.newImageRect( "images/airplane.png", 70, 45 );  
plane.x = 880 ;  
plane.y = 60 ;  
  
local animPlane;  
animPlane = transition.to (plane, {  
 time = 4000,  
 x = -80,  
});  

and this not:

local plane = display.newImageRect( "images/airplane.png", 70, 45 );  
plane.x = 880 ;  
plane.y = 60 ;  
  
function movePlane ()  
 plane.x = plane.x - 5  
end  
  
for loop = 0, 180 do  
 timer.performWithDelay(1000, movePlane)  
end  
  

the first code works,
i was hoping the second codes will also make the plane move from right to left,
but what happen was it showed the beginning position and after a while just jump to end position.

P.S:
sorry for the noob question :slight_smile: well I have to start somewhere :slight_smile: [import]uid: 31508 topic_id: 31696 reply_id: 331696[/import]

You should not be using a loop here- you are starting 180 different timers and not giving time to update as a result.

Just use one timer, firing 180 times, like so;

[lua] timer.performWithDelay(1000, movePlane,180)[/lua]

That will fix it :slight_smile: (though you may want to speed it up from 1000)

Peach :slight_smile: [import]uid: 52491 topic_id: 31696 reply_id: 126624[/import]

Thanks Peach :slight_smile: [import]uid: 31508 topic_id: 31696 reply_id: 126625[/import]

Looks like you’re trying to move the plane in steps instead of defining a whole transition. To do this properly, I would add it to the Runtime listener and calculate how far it’s supposed to move every frame, based on the time:

local plane = display.newImageRect( "images/airplane.png", 70, 45 );  
plane.x = 880 ;  
plane.y = 60 ;  
  
--CALCULATE Xstep  
local frameRate = 60  
local moveTime = 4000  
local moveDist = -80  
local fpms = 0.001 -- FRAMES PER MILLISECOND  
local totFrames = frameRate \* moveTime \* fpms -- TOTAL FRAMES TO MOVE  
local Xstep = moveDist / totFrames -- PIXELS MOVED PER FRAME  
  
function movePlane()  
 local count = 1 --START COUNT AT 1  
  
 local function move()  
 if (count \< totFrames) then   
 plane.x = plane.x + Xstep  
 count = count +1  
 else Runtime:removeEventListener('enterFrame', move) end  
 end  
  
 --MOVE PLANE BY Xstep ON EVERY FRAME  
 Runtime:addEventListener('enterFrame', move)  
end  
  
movePlane()  

This is equivalent to using transition.to(plane, {time=4000, x=-80}). However, it allows you more control because you can add code to change Xstep while it’s running, pause it, etc. When it’s done running, the local function gets removed from memory. Careful not to call movePlane() more than once, b/c you would then double up the Runtime and move the plane twice per frame.

Hope that’s useful :slight_smile: [import]uid: 36792 topic_id: 31696 reply_id: 126640[/import]

You should not be using a loop here- you are starting 180 different timers and not giving time to update as a result.

Just use one timer, firing 180 times, like so;

[lua] timer.performWithDelay(1000, movePlane,180)[/lua]

That will fix it :slight_smile: (though you may want to speed it up from 1000)

Peach :slight_smile: [import]uid: 52491 topic_id: 31696 reply_id: 126624[/import]

Thanks Peach :slight_smile: [import]uid: 31508 topic_id: 31696 reply_id: 126625[/import]

Looks like you’re trying to move the plane in steps instead of defining a whole transition. To do this properly, I would add it to the Runtime listener and calculate how far it’s supposed to move every frame, based on the time:

local plane = display.newImageRect( "images/airplane.png", 70, 45 );  
plane.x = 880 ;  
plane.y = 60 ;  
  
--CALCULATE Xstep  
local frameRate = 60  
local moveTime = 4000  
local moveDist = -80  
local fpms = 0.001 -- FRAMES PER MILLISECOND  
local totFrames = frameRate \* moveTime \* fpms -- TOTAL FRAMES TO MOVE  
local Xstep = moveDist / totFrames -- PIXELS MOVED PER FRAME  
  
function movePlane()  
 local count = 1 --START COUNT AT 1  
  
 local function move()  
 if (count \< totFrames) then   
 plane.x = plane.x + Xstep  
 count = count +1  
 else Runtime:removeEventListener('enterFrame', move) end  
 end  
  
 --MOVE PLANE BY Xstep ON EVERY FRAME  
 Runtime:addEventListener('enterFrame', move)  
end  
  
movePlane()  

This is equivalent to using transition.to(plane, {time=4000, x=-80}). However, it allows you more control because you can add code to change Xstep while it’s running, pause it, etc. When it’s done running, the local function gets removed from memory. Careful not to call movePlane() more than once, b/c you would then double up the Runtime and move the plane twice per frame.

Hope that’s useful :slight_smile: [import]uid: 36792 topic_id: 31696 reply_id: 126640[/import]

Thank you ArdentKid,

I’ve tested the code,
it didn’t work so I ‘tweak’ a little at line 11:

local Xstep = ( moveDist - plane.x ) / totFrames  

what do you think? [import]uid: 31508 topic_id: 31696 reply_id: 128056[/import]

docs recommend object:translate() for changing positions of display objects
http://docs.coronalabs.com/api/type/DisplayObject/translate.html

so instead of

 obj.x = obj.x + xmove  
 obj.y = obj.y + ymove  

use this

 obj:translate(xmove,ymove)  

when having only few objects to move around, this tweak won’t boost perfomance very much.
but that changes when you have a lot of objects to move in a single frame.

-finefin [import]uid: 70635 topic_id: 31696 reply_id: 128059[/import]

bpran - yes, adjust it to your needs :slight_smile:

I believe using translate() is more efficient, think it has something to do with the GPU not having to re-draw the object every frame. [import]uid: 36792 topic_id: 31696 reply_id: 128174[/import]

Thank you ArdentKid,

I’ve tested the code,
it didn’t work so I ‘tweak’ a little at line 11:

local Xstep = ( moveDist - plane.x ) / totFrames  

what do you think? [import]uid: 31508 topic_id: 31696 reply_id: 128056[/import]

docs recommend object:translate() for changing positions of display objects
http://docs.coronalabs.com/api/type/DisplayObject/translate.html

so instead of

 obj.x = obj.x + xmove  
 obj.y = obj.y + ymove  

use this

 obj:translate(xmove,ymove)  

when having only few objects to move around, this tweak won’t boost perfomance very much.
but that changes when you have a lot of objects to move in a single frame.

-finefin [import]uid: 70635 topic_id: 31696 reply_id: 128059[/import]

bpran - yes, adjust it to your needs :slight_smile:

I believe using translate() is more efficient, think it has something to do with the GPU not having to re-draw the object every frame. [import]uid: 36792 topic_id: 31696 reply_id: 128174[/import]