[Resolved]transition.to( ) problem (situation too complicated for a less ambiguous title)

I think I overcomplicated things for myself again.

Basically I want an image to move in a square path on the left side of the screen, and when you click on it, to move X number of pixels to the right and continue moving in a square path.

What I have now moves the image in a square path on the left, then when I click it it moves to the right, then just stops there.

the functions in moveBlueLoop are in order of how the blue square goes through them, that’s why, for example, in the function called moveBlueTopRight( ), if the square is on the right hand side, the transition.to( ) function sends it to the bottom left, instead of the top right, like the name suggests - because, when the square finishes moving right, from the moveRight( ) function, I want it to go to the bottom left corner first, before starting looping on the path (yes, the path on the left goes clockwise, the one on the right, anti-clockwise, I want it that way, it’s not a mistake.)

The code is plug n’ play. Press red to start the loop, and blue, while its moving, to make it move to the right

[lua]display.setStatusBar( display.HiddenStatusBar)

local executeOnce = true
local moving = false

local screenTop = display.screenOriginY
local screenBottom = display.viewableContentHeight + display.screenOriginY
local screenLeft = display.screenOriginX
local screenRight = display.viewableContentWidth + display.screenOriginX

local red = display.newRect( 0, 0, 50, 50 )
red:setFillColor(255,0,0)
local blue = display.newRect( 80, 30, 50, 50 )
blue:setFillColor(0,0,255)

path1 = {}
path1.TopLeftX = 96
path1.TopLeftY = 46
path1.TopRightX = 106
path1.TopRightY = 46
path1.BottomRightX = 106
path1.BottomRightY = 184
path1.BottomLeftX = 96
path1.BottomLeftY = 184

path2 = {}
path2.TopLeftX = 206
path2.TopLeftY = 46
path2.TopRightX = 216
path2.TopRightY = 46
path2.BottomRightX = 216
path2.BottomRightY = 184
path2.BottomLeftX = 206
path2.BottomLeftY = 184

local transitions1 = {}
local transitions2 = {}

local mapPart = 1

local moveBlueBottomRight
local moveBlueBottomLeft
local moveBlueTopLeft

local function moveBlueLoop(event)
local function moveBlueTopRight(event)
if (mapPart == 1) then
transitions1[#transitions1 + 1] = transition.to(blue, {
time = 150,
x = path1.TopRightX,
y = path1.TopRightY,
onComplete = moveBlueBottomRight
})
else if (mapPart == 2) then
transitions2[#transitions2 + 1] = transition.to(blue, {
time = 1500,
x = path2.BottomLeftX,
y = path2.BottomLeftY,
onComplete = moveBlueBottomRight
})
end
end
end

moveBlueBottomRight = function(event)
if (mapPart == 1) then
transitions1[#transitions1 + 1] = transition.to(blue, {
time = 1500,
x = path1.BottomRightX,
y = path1.BottomRightY,
onComplete = moveBlueBottomLeft
})
else if (mapPart == 2) then
transitions1[#transitions2 + 1] = transition.to(blue, {
time = 150,
x = path2.BottomRightX,
y = path2.BottomRightY,
onComplete = moveBlueBottomLeft
})
end
end
end

moveBlueBottomLeft = function(event)
if (mapPart == 1) then
transitions1[#transitions1 + 1] = transition.to(blue, {
time = 150,
x = path1.BottomLeftX,
y = path1.BottomLeftY,
onComplete = moveBlueTopLeft
})
else if (mapPart == 2) then
transitions2[#transitions2 + 1] = transition.to(blue, {
time = 1500,
x = path2.TopRightX,
y = path2.TopRightY,
onComplete = moveBlueTopLeft
})
end
end
end

moveBlueTopLeft = function(event)
if (mapPart == 1) then
transitions1[#transitions1 + 1] = transition.to(blue, {
time = 1500,
x = path1.TopLeftX,
y = path1.TopLeftY,
onComplete = moveBlueTopRight
})
else if (mapPart == 2) then
transitions2[#transitions2 + 1] = transition.to(blue, {
time = 150,
x = path2.TopLeftX,
y = path2.TopLeftY,
onComplete = moveBlueTopRight
})
end
end
end
if (executeOnce == true) then
moveBlueTopRight()
executeOnce = false
end
end

red:addEventListener(“tap”, moveBlueLoop)

local function moveRight(event)
if (mapPart == 1) then
for i = 1,#transitions1 do
if transitions1[i] then
transition.cancel(transitions1[i])
transitions1[i] = nil
end
end

Runtime:removeEventListener(“enterFrame”, moveBlueLoop)

if (blue.x > 85) then
transition.to(blue,{
time = 300,
x = 206,
y = blue.y,
onComplete = moveBlueTopRight
})
else
transition.to(blue,{
time = 310,
x = 216,
y = blue.y,
onComplete = moveBlueTopRight
})
end

mapPart = 2

else if (mapPart == 2) then
for i = 1,#transitions2 do
if transitions2[i] then
transition.cancel(transitions2[i])
transitions2[i] = nil
end
end

Runtime:removeEventListener(“enterFrame”, moveBlueLoop)

if (blue.x > 85) then
transition.to(blue,{
time = 300,
x = 206,
y = blue.y
})
else
transition.to(blue,{
time = 310,
x = 216,
y = blue.y
})
end

end
end
end

blue:addEventListener(“tap”, moveRight)[/lua] [import]uid: 106739 topic_id: 18308 reply_id: 318308[/import]

Don’t know if this is the cause but check out lines 138 & 166. You are removing “Runtime” listener but your listener is red:addEventListener(“tap”, moveBlueLoop). [import]uid: 40033 topic_id: 18308 reply_id: 70171[/import]

That’s because the way I understand listeners and removing them, is, when I remove a listener like red:removeEventListener(“tap”, moveBlueLoop), I tell the program “if the user taps on the red square, don’t update moveBlueLoop” (which wouldn’t resolve the issue cause the user isn’t gonna tap the red square again anyway, what needs to happen is for the undergoing transitions to stop), while what I want to tell it is “stop updating moveBlueLoop in every consecutive frame”, which is what the program is doing since I activated it by tapping on it.

Am I wrong?

Plus, changing those two lines doesn’t resolve the issue. [import]uid: 106739 topic_id: 18308 reply_id: 70173[/import]

I resolved it by changing lines 145 and 152 to onComplete = moveCompensation and adding this at the end

[lua]moveCompensation = function(event)
transition.to(blue,{
time = 700,
x = path2.BottomLeftX,
y = path2.BottomLeftY,
onComplete = moveBlueBottomRight
})
end[/lua]

if anyone has any other, more elegant way of doing it, let me know. :slight_smile: [import]uid: 106739 topic_id: 18308 reply_id: 70176[/import]

Glad you found it!

(My point on the remove listener is that the orig listener is on the object red; it’s not a runtime listener.) [import]uid: 40033 topic_id: 18308 reply_id: 70179[/import]