Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

transition.moveto oncomplete question
Started by iansingleton39 Nov 24 2018 03:18 AM

2 replies to this topic
oncomplete transition moveto function rocket move

Best Answer XeduR @Spyric , 25 November 2018 - 04:35 AM

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 )

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

iansingleton39

[GLOBAL: userInfoPane.html]
iansingleton39
  • Observer

  • 2 posts
  • Corona SDK

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.

 

 



[TOPIC: post.html]
#2

Quitalizner

[GLOBAL: userInfoPane.html]
Quitalizner
  • Enthusiast

  • 30 posts
  • Corona SDK

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 )


[TOPIC: post.html]
#3

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 368 posts
  • Corona SDK

  Best Answer

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 )



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: oncomplete, transition, moveto, function, rocket, move