transition.to How can I really update the X and Y coordinates when moving?

I have a board with X and Y coordinates and I need to move a ball through it using the transition.to. The problem is when transition run it doesn’t really change the ball x and y positions… For instance:

Original ball position before the transition:
ball.x is 0
ball.y is 20

transition.to(ball, {time=200, x=20, y=40})  

After transition, when I get the ball.x and ball.y it is still 0 and 20 respectively. And if I use onComplete to try to change the ball.x and ball.y, it doesn’t work either. [import]uid: 159026 topic_id: 34485 reply_id: 334485[/import]

Did you try specify a function for the onComplete parameter that changes the ball’s position?

local function changeCoordinates(self) self.x = 20; self.y = 40; end transition.to(ball, {time=200, x=20, y=40, onComplete=changeCoordinates}) [import]uid: 14018 topic_id: 34485 reply_id: 137101[/import]

Hi Mitaten, thanks for the reply. Yes, I did this but I think I found the problem, although I don’t know the solution. Let me explain: I have a function that is called every time I press a button:

function go()   
 while canWalk \> 0 do  
 if isAllowed( boardRow, boardColumn+1) then  
 move( boardRow, boardColumn+1)  
 else  
 canWalk = 0  
 end  
 end  
end  
  
function isAllowed(allowedX,allowedY)  
 if board[allowedX][allowedY].isObstacle == 1 then  
 print(" can't walk anymore. ")  
 return false  
 end  
 return allowedX \< numOfRows and allowedY \< numOfColumns  
end  
  
function move(moveX,moveY)  
  
 coordinateX = board[moveX][moveY].x + (openTileWidth/2)  
 coordinateY = board[moveX][moveY].y + (openTileHeight/2)  
  
 transition.to(ball, {time=movementTime, x=coordinateX, y=coordinateY, onComplete=changeCoordinates})  
end  
  
function changeCoordinates()  
 ball.x = coordinateX  
 ball.y = coordinateY  
end  

Situation 1: If I remove the while canWalk > 0 it works.
Situation 2: If I keep the “while” Corona stays inside the loop forever.
Situation 3: If I keep the “while” but remove the “transition.to”, and only update the ball coordinates directly it works.

--Situation 3:  
function move(moveX,moveY)  
 ball.x = board[moveX][moveY].x + (openTileWidth/2)  
 ball.y = board[moveX][moveY].y + (openTileHeight/2)  
end  

It seems that there is something with transition. Is there a way to wait for transition before the while loop run again?

Thanks! [import]uid: 159026 topic_id: 34485 reply_id: 137104[/import]

Hi rodrigocamargo1. You’re constantly calling transition via the loop, which is probably making the ball stay in the same first “frame” of the transition. This might not be what you’re trying to do, but maybe something more like the following:

[lua]-- on button press somewhere
destinationX = ball.x + (openTileWidth/2)*5
destinationY = ball.y
nextPosition()

function move(moveX,moveY)
transition.to(ball, {time=movementTime, x=moveX, y=moveY, onComplete=nextPosition})
end

function nextPosition()
if(moveX >= destinationX && moveY >= destinationY) then return end
– just whatever you want to break out

move(ball.x+(openTileWidth/2), ball.y+(openTileHeight/2))
end [/lua] [import]uid: 178312 topic_id: 34485 reply_id: 137108[/import]

Thanks a lot yipperpants! It works! I didn’t think about recursive call. Thanks a lot for the tip! [import]uid: 159026 topic_id: 34485 reply_id: 137109[/import]

No problem, glad I could help. Best of luck with your project. (: [import]uid: 178312 topic_id: 34485 reply_id: 137110[/import]

Did you try specify a function for the onComplete parameter that changes the ball’s position?

local function changeCoordinates(self) self.x = 20; self.y = 40; end transition.to(ball, {time=200, x=20, y=40, onComplete=changeCoordinates}) [import]uid: 14018 topic_id: 34485 reply_id: 137101[/import]

Hi Mitaten, thanks for the reply. Yes, I did this but I think I found the problem, although I don’t know the solution. Let me explain: I have a function that is called every time I press a button:

function go()   
 while canWalk \> 0 do  
 if isAllowed( boardRow, boardColumn+1) then  
 move( boardRow, boardColumn+1)  
 else  
 canWalk = 0  
 end  
 end  
end  
  
function isAllowed(allowedX,allowedY)  
 if board[allowedX][allowedY].isObstacle == 1 then  
 print(" can't walk anymore. ")  
 return false  
 end  
 return allowedX \< numOfRows and allowedY \< numOfColumns  
end  
  
function move(moveX,moveY)  
  
 coordinateX = board[moveX][moveY].x + (openTileWidth/2)  
 coordinateY = board[moveX][moveY].y + (openTileHeight/2)  
  
 transition.to(ball, {time=movementTime, x=coordinateX, y=coordinateY, onComplete=changeCoordinates})  
end  
  
function changeCoordinates()  
 ball.x = coordinateX  
 ball.y = coordinateY  
end  

Situation 1: If I remove the while canWalk > 0 it works.
Situation 2: If I keep the “while” Corona stays inside the loop forever.
Situation 3: If I keep the “while” but remove the “transition.to”, and only update the ball coordinates directly it works.

--Situation 3:  
function move(moveX,moveY)  
 ball.x = board[moveX][moveY].x + (openTileWidth/2)  
 ball.y = board[moveX][moveY].y + (openTileHeight/2)  
end  

It seems that there is something with transition. Is there a way to wait for transition before the while loop run again?

Thanks! [import]uid: 159026 topic_id: 34485 reply_id: 137104[/import]

Hi rodrigocamargo1. You’re constantly calling transition via the loop, which is probably making the ball stay in the same first “frame” of the transition. This might not be what you’re trying to do, but maybe something more like the following:

[lua]-- on button press somewhere
destinationX = ball.x + (openTileWidth/2)*5
destinationY = ball.y
nextPosition()

function move(moveX,moveY)
transition.to(ball, {time=movementTime, x=moveX, y=moveY, onComplete=nextPosition})
end

function nextPosition()
if(moveX >= destinationX && moveY >= destinationY) then return end
– just whatever you want to break out

move(ball.x+(openTileWidth/2), ball.y+(openTileHeight/2))
end [/lua] [import]uid: 178312 topic_id: 34485 reply_id: 137108[/import]

Thanks a lot yipperpants! It works! I didn’t think about recursive call. Thanks a lot for the tip! [import]uid: 159026 topic_id: 34485 reply_id: 137109[/import]

No problem, glad I could help. Best of luck with your project. (: [import]uid: 178312 topic_id: 34485 reply_id: 137110[/import]