transition.moveto oncomplete question

local rocket = display.newImageRect( "spriteFolder/set\_10.png",16,16 ) rocket.x = 8 ; rocket.y = 296 rocket.myName = "rocket" physics.addBody( rocket, "static") local square = display.newRect( 435, 25, 50, 50 ) ---------------------- local touchStarted = false local moveRocket = function(event) local function moveRight( ) if touchStarted then local xx = rocket.x local xx = xx+16 print("in functon".."--xx = "..xx) transition.moveTo( rocket, { x=xx, y=296, time=50, onComplete } ) end end if event.phase == 'began' then touchStarted = true elseif ( event.phase == "ended" ) then touchStarted = false end timer.performWithDelay(100,moveRight,0) end square:addEventListener( "touch", moveRocket )

im trying to create a function that moves a rocket object left and right, im currently working on moving right…the idea is to start at x = 8, when you press and and hold a button it will add 16 and move to, althought it is running to fast through the process… i think i should be using oncomplete, but im at a loss how to incorporate this into my function.

I don’t completely understand what you want to do with onComplete in transition, but if you want to slow down the movement you can use this.

Try to increase the time if you want to object to move slowly:-

transition.moveTo( rocket, { x=xx, y=296, time=2000} )

Or change your code to this:-

local function moveRight( ) local xx = rocket.x xx = xx+16 print("in functon".."--xx = "..xx) transition.moveTo( rocket, { x=xx, y=296, time=500} ) end local rocketTimer local moveRocket = function(event) if event.phase == 'began' then rocketTimer = timer.performWithDelay(500,moveRight,0) elseif event.phase == 'ended' then timer.cancel(rocketTimer) end return true end square:addEventListener( "touch", moveRocket )

I haven’t used your approach to move objects like this in my projects, but you could clean it up the following way:

local rocket = display.newImageRect( "spriteFolder/set\_10.png",16,16 ) rocket.x = 8 ; rocket.y = 296 rocket.myName = "rocket" physics.addBody( rocket, "static") local square = display.newRect( 435, 25, 50, 50 ) ---------------------- local touchStarted = false local rocketTransition local rocketMove = 16 local rocketTime = 50 local moveRocket = function(event) local function moveRight() if touchStarted then rocketTransition = transition.moveTo( rocket, { x=rocket.x+rocketMove, y=rocket.y, time=rocketTime, onComplete=moveRight }) end end if event.phase == 'began' then touchStarted = true moveRight() elseif ( event.phase == "ended" ) then touchStarted = false end end square:addEventListener( "touch", moveRocket )

I don’t completely understand what you want to do with onComplete in transition, but if you want to slow down the movement you can use this.

Try to increase the time if you want to object to move slowly:-

transition.moveTo( rocket, { x=xx, y=296, time=2000} )

Or change your code to this:-

local function moveRight( ) local xx = rocket.x xx = xx+16 print("in functon".."--xx = "..xx) transition.moveTo( rocket, { x=xx, y=296, time=500} ) end local rocketTimer local moveRocket = function(event) if event.phase == 'began' then rocketTimer = timer.performWithDelay(500,moveRight,0) elseif event.phase == 'ended' then timer.cancel(rocketTimer) end return true end square:addEventListener( "touch", moveRocket )

I haven’t used your approach to move objects like this in my projects, but you could clean it up the following way:

local rocket = display.newImageRect( "spriteFolder/set\_10.png",16,16 ) rocket.x = 8 ; rocket.y = 296 rocket.myName = "rocket" physics.addBody( rocket, "static") local square = display.newRect( 435, 25, 50, 50 ) ---------------------- local touchStarted = false local rocketTransition local rocketMove = 16 local rocketTime = 50 local moveRocket = function(event) local function moveRight() if touchStarted then rocketTransition = transition.moveTo( rocket, { x=rocket.x+rocketMove, y=rocket.y, time=rocketTime, onComplete=moveRight }) end end if event.phase == 'began' then touchStarted = true moveRight() elseif ( event.phase == "ended" ) then touchStarted = false end end square:addEventListener( "touch", moveRocket )